MCPcopy Index your code
hub / github.com/careercup/ctci / drawLine

Method drawLine

java/Chapter 5/Question5_8/Question.java:10–45  ·  view source on GitHub ↗
(byte[] screen, int width, int x1, int x2, int y)

Source from the content-addressed store, hash-verified

8 }
9
10 public static void drawLine(byte[] screen, int width, int x1, int x2, int y) {
11 int start_offset = x1 % 8;
12 int first_full_byte = x1 / 8;
13 if (start_offset != 0) {
14 first_full_byte++;
15 }
16
17 int end_offset = x2 % 8;
18 int last_full_byte = x2 / 8;
19 if (end_offset != 7) {
20 last_full_byte--;
21 }
22
23 // Set full bytes
24 for (int b = first_full_byte; b <= last_full_byte; b++) {
25 screen[(width / 8) * y + b] = (byte) 0xFF;
26 }
27
28 byte start_mask = (byte) (0xFF >> start_offset);
29 byte end_mask = (byte) ~(0xFF >> (end_offset + 1));
30
31 // Set start and end of line
32 if ((x1 / 8) == (x2 / 8)) { // If x1 and x2 are in the same byte
33 byte mask = (byte) (start_mask & end_mask);
34 screen[(width / 8) * y + (x1 / 8)] |= mask;
35 } else {
36 if (start_offset != 0) {
37 int byte_number = (width / 8) * y + first_full_byte - 1;
38 screen[byte_number] |= start_mask;
39 }
40 if (end_offset != 7) {
41 int byte_number = (width / 8) * y + last_full_byte + 1;
42 screen[byte_number] |= end_mask;
43 }
44 }
45 }
46
47 public static void printByte(byte b) {
48 for (int i = 7; i >= 0; i--) {

Callers 1

mainMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected