Just being able to change the color of a large square window is not going to get us very far so next I will show you how to plot a point. By default the Playpen behaves like a sheet of graph paper with the origin in the center. Do not worry if you have forgotten those math lessons about plotting points because the program will do most of the work. There are lots of colors available but let me stick with black and white for now. If you feel adventurous you can replace my black and white with color mixtures or numbers (I will call those palette codes from now on).
Create a new project, like the one you did before but call this onefirst plot. Just a quick reminder as to how to do this:
1) Open Quincy.
2) Go to the File menu and selectNew. 3) SelectProject.
4) Make sure that you are going to put it into theChapter 1directory. 5) Change the name from the one Quincy guessed tofirst plot. 6) Set the options as we did above.
7) Now insert the two library files (libgdi32.aandfgwlib.a) making sure they are in the right order in the list:fgwlib.afirst thenlibgdi32.a.
8) Save the project file.
Next start a new source code file. Call itplot. Now copy in the following source code and compile it (F5). When it compiles successfully add this file to the project and press F9. You should get a white playpen with a tiny black dot in the middle. Yes, it really is tiny, small enough so that 512 of them would fit side by side across the Playpen.
// point plotting program // on
#include "playpen.h" #include <iostream> using namespace fgw; using namespace std;
int main(){
playpen paper(white); paper.plot(0, 0, black); paper.display();
cout << "press RETURN"; cin.get();
}
Notice how those twousingstatements (correctly called ‘‘usingdirectives’’) simplify what we have to write subsequently. You may wonder why we provide the prefixes if we then promptly remove them. The answer is that, just like our family names, they are there when we want or need to be more specific.
Wouldn’t it be nice if we could make that black point bigger? Well you could patiently add the following lines immediately afterpaper.plot(0, 0, black);
paper.plot(0, 1, black); paper.plot(1, 0, black); paper.plot(1, 1, black);
If you are anything like me you would prefer not to do all that work, even if most of it is done by cut and paste. If you want to make the point four times wider and higher you would have to plot 16 points. That seems excessive. There is a better way; you can change the scale of the display with the following statement:
paper.scale(2);
You can use any number from 1 to 64 as the scale. If you try numbers outside that range they will be ignored. Changing the scale changes both the size and the position at which a point is plotted. So when you use a scale of 2, the pixel is twice as high, twice as wide and twice as far from the origin compared to using a scale of 1.
Instead of plotting black points you can choose any palette code from 0 to 255. By convention 0 is black and 255 is white.
Create a new project for the following small program. Please do not skimp by reusing the one you have. You need to become fluent in starting a project so that the process becomes second nature, including checking that you save files where they belong.
// point plotting & scaling program // on #include "playpen.h" #include <iostream> using namespace fgw; using namespace std; int main(){ playpen paper(white); paper.scale(8); paper.plot(0, 0, black); paper.scale(4); paper.plot(0, 0, white); paper.display();
cout << "press RETURN to end"; cin.get();
}
Please remember to get the source code to compile by using F5 before you add the file to the project. This is just a good habit and helps you focus on one thing at a time. Once it is in the project you can still edit the source code, but you know that it has compiled successfully. If it stops doing so you know that it is something you just did that caused the problem.
Now experiment with this program (remember the warning I gave earlier when experimenting; make sure you finish a run of a program before you try to build – F9 – a new version). Use some different scales, some different palette codes or color mixes and some different coordinates for the points instead of the 0, 0 that we have been using.
You should notice that when you plot at other places the two plots may no longer overlap. Actually you will not even see the second one if you continue to use white for it because you will be plotting a white pixel on a white background.