Skip to content

Instantly share code, notes, and snippets.

@bitcpf
Created August 5, 2014 17:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bitcpf/5a7b7b9f08f0d47b1b8d to your computer and use it in GitHub Desktop.
Save bitcpf/5a7b7b9f08f0d47b1b8d to your computer and use it in GitHub Desktop.
public class Q5_8 {
public void drawHLine(byte[] screen, int width, int x1, int x2, int y) {
int height = screen.length / (width / 8);
if(y < 0 || y >= height || x1<0 || x2 <0 || x1 > screen.length * 8 || x2 > screen.length * 8)
{
return;
}
int idx = y*(width/8);
int idx1 = idx + (x1/8);
int idx2 = idx + (x2/8);
for (int i = idx1+1; i< idx2; i++){
screen[i] = (byte)0xFF;
}
// mask for the start offset, before the start offset set 0, after is 1
byte mask1 = (byte) (0xff >>> x1 % 8); //>>> no sign shift right
// mask for the end offset, after the end offset set 0, before 1
byte mask2 = (byte) (~((1 << (7 - x2 % 8)) - 1));
if (idx1 == idx2) {
screen[idx1] |= (mask1 & mask2);
} else {
screen[idx1] |= mask1;
screen[idx2] |= mask2;
}
}
public static void main(String[] args) {
Q5_8 q5_8 = new Q5_8();
byte[] screen = new byte[48];
int width = 8;
for (int i = 0; i < screen.length / width; ++i) {
for (int j = 0; j < width; ++j) {
System.out.print(screen[i * width + j]);
}
System.out.println();
}
System.out.println();
q5_8.drawHLine(screen, width * 8, 9, 26, 1);
for (int i = 0; i < screen.length / width; ++i) {
for (int j = 0; j < width; ++j) {
System.out.print(screen[i * width + j]);
}
System.out.println();
}
System.out.println();
screen = new byte[12];
q5_8.drawHLine(screen, width * 8, 1, 5, 0);
for (int i = 0; i < screen.length / width; ++i) {
for (int j = 0; j < width; ++j) {
System.out.print(screen[i * width + j]);
}
System.out.println();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment