class FrameBuffer { public: FrameBuffer(int width, int height); void setPixel(int x, int y, bool on = true); void display(ostream &os = cout); int getWidth() const; int getHeight() const; private: int height, width; bool **pixels; };Notes:
setPixel
turnfs on/off the pixel in the buffer at location/position
x
, y
.
display
sends the contents of the buffer to the specified output
(defaulting to cout
).
bool
). Since the dimensions
are not known at compile time, we declare the corresponding variable as
bool **
, i.e., a pointer (array) to a pointer (array) of bool
.
In order to properly allocate such an array using new
, we first allocate
the array of arrays, and then create the individual arrays of bool
s.
Rather than giving you the exact code, here is a very close pseudocode version:
pixels = Create a bool * array ofwidth
elements for (int i = 0; i < width; i++) Create abool
array ofheight
elements and assign it topixels[i]
Here is a sample program that produces this output
drawLine(x1, y1, x2, y2) slope = (y2 - y1) / (x2 - x1) // slope is not an integer y = y1 for x = x1 to x2 drawPoint(x, y) y = y + slope // need to either truncate, ceiling, or roundNotes:
slope = (y2 - y1) / (x2 - x1)make sure you use floating point division-- integer division will produce an integer slope which will be VERY boring and unsatisfying.
y + slope
will typically not evaluate to an integer (since
the slope is not an integer-- see the previous note). Your choices are to
round
function)
class Graphic { public: Graphic(int width, int height); void ddaLine(int x1, int y1, int x2, int y2); private: FrameBuffer fb; };Notes:
Graphic
accepts a resolution and creates the appropriate
FrameBuffer object.
drawLine
implements the dda line drawing algorith displayed above;
the frame buffer is displayed after the line is drawn.
Make sure your algorith works for the following types of lines:
Here is a sample program that produces this output
Modify the frame buffer to accept color pixels. If you wish, come up with your own scheme;
alternatively, feel free to copy what I did which is to allow 10 colors -- 0 being black
(absence of color) and 1-9 representing 9 other colors. The Graphic
class
must also change to reflect this modification. Here are the new class declarations
(you, obviously are to finsih off the implementations):
class FrameBuffer { public: FrameBuffer(int width, int height); void setPixel(int x, int y, int color); void display(ostream &os = cout); int getWidth() const {return width;} int getHeight() const {return height;} private: int height, width; int **pixels; }; class Graphic { public: Graphic(int width, int height); void drawLine(int x1, int y1, int x2, int y2, int color); private: FrameBuffer fb; };Again, feel free to come up with your own color scheme. Remember that each pixel probably takes up only one character when displayed and thus whatever color representation you decide on should have the color represented by a single character (which is why I limited myself to the integers 0-9-- representable by single-characters).
Here is a sample program that produces this output
One possibility is to blend the colors, and in particular, to have the frame buffer blend the colors.
Modify the FrameBuffer
class so that when setPixel
is called on a pixel
that is already on (i.e., not black or color 0), the new color value is blended with
the current pixel color.
I blended my colors by taking the average of the new and current color and translating it to the corresponding letter 'A' for 1, 'B' for 2, etc. How YOU blend the color is up to you-- come up with your own scheme, or use mine or some variant.
Notice that the user does not change their application here. We COULD provide a function either in the Graphic
and/or FrameBuffer
class that allows the user to turn on and off color blending.
Here is a sample program (same as Part 3) that produces this output
r2 = y2 + y2There are three basic variations:
x = r cos(t) y = r sin(t) where t varies from 0 to 2 * PICode each of the above algorithms and see how good a circle you get. Try an algorithm that calls the x-varies followed by the y-varies and seem what that does.
Add a function to your Graphic
class that allows the user to select which circle algorithm they wish to use.
You don't really have to do the calculation on the ENTIRE circle-- calculate the first quadrant-- the others are mirror images around one or both axes (i.e., reverse the sign of the x and/or y coordinate of the resulting point. In actuality, all you really need is one either of the circle in order to use an approach like this). See if you can figure out how to do this.
The original algorithm was for line segments, but was extended to a circle version (look under either Bresenham or midpoint).
Implement Bresenham's algorithm for lines and circles. As in the previous part, provide functionality to allow the user to select which algorithm to use.
Play with the same various slope types as described in Part 2. Try drawing the same line with DDA and Bresenham (with color blending) and see how they compare.
Assignment01
- Each part of the assignment should reside in its own directory,
Part**
beneath a single directory
where **
is 01, 02, 03, .., 07
*** CIS 3620 *** Assignment 01 SUBMISSION
.
Please do not use a different subject heading
*** CIS 3620 *** Assignment 01 HELP
.