Exercises Computer Graphics

Les 2 - Bitoperations Oplossingen

Solutions

Exercises:

Useful Links:

Introduction

Solutions for session 2.

Exercises

Exercises 1 and 2: Vertical and Horizontal Lines

Drawing horizontal and vertical lines: ex2solution.pde

public void drawVertical(int x, int y1, int y2, int colour) {
  int mask = 0xff<<(3-x%4)*8;
  colour = colour << (3-x%4)*8;
  for (int i=y1*width/4; i<y2*width/4; i+=width/4)
    s.put(x/4+i, (s.get(x/4+i)&~mask) | colour); 
  }
}

public void drawHorizontal(int x1, int x2, int y, int colour) { 
int m1, m2; // begin+end of line

colour = colour<<24 | colour<<16 | colour<<8 | colour; // 4 bytes with color
m1 = 0xffffffff >>> ((x1%4)*8); // mask for begin of horz line
// >>> = unsigned shift right
m2 = 0xffffffff << ((3-x2%4)*8); // mask for end of horz line

if (x1/4==x2/4) // short line
s.put(x1/4+y*width/4, (s.get(x1/4+y*width/4)&~(m1&m2)) | ((m1&m2)&colour));
else {
s.put(x1/4+y*width/4, (s.get(x1/4+y*width/4)&~m1) | (m1&colour));
s.put(x2/4+y*width/4, (s.get(x2/4+y*width/4)&~m2) | (m2&colour));
for (int i=(x1/4)+1; i<(x2/4); i++)
s.put(y*width/4+i, colour);
}
}

Valid HTML 4.01 Transitional ©2008-2009 Tom Van Cutsem