Why start a library when the commands are that simple, Pen Up, Pen Down, Pen Absolute 200,300...?
Because we want to get the plotter commands from coding and the Serial commands are Strings, pieces of text. We have variable numbers for the points. We don't want to put all the points of a Lissajou figure in a piece of text by hand, do we?
So we make our first function for drawing a line
void myLine(int p1x, int p1y, int p2x, int p2y)
{
//go to spot p1
myPort.write ( "PU;PA" + str(p1x) + "," + str( p1y) + ";");
//draw line to spot p2
myPort.write ( "PD;PA" + str(p2x) + "," + str( p2y) + ";PU;");
}
This functions takes care of making the right string and sends it to the plotter. The variable point coordinates are included in the string using the str() function. This function makes a String from a number. So instead of 5 like a number str(5) is like "5" as text.
We could stop here and we would already having a lot of fun. For the students who are very brave explorers we go on:
What could we add to our library?
Well in our world of rectangles, in fact nearly all objects around us are rectangular, refrigerators, books, chairs, tables, we need a function to plot a rectangle. But couldn't we just draw a rectangle like this?
myLine(0, 0, 0, 200);
myLine(0, 200, 200, 200);
myLine(200, 200, 200, 0);
myLine(200, 0, 0, 0);
Yes of course, but what about draw 100 rectangles, are you going to do this every time using these four lines of code? We just want one function!
function myRectangle ( point leftTopPoint, point rightTopPoint, point rightBottomPoint, point leftBottomPoint)
{
myLine(leftTopPoint, rightTopPoint);
myLine(rightTopPoint, rightBottomPoint);
myLine(rightBottomPoint, leftBottomPoint);
myLine(leftBottomPoint, leftTopPoint);
}
But now we see it is would be great to have something which is "a point", consisting of two coordinates, x and y. This point should be accepted by our first function myLine. So we have to enlarge the possibilities of myLine.
This is just what I have done, adding Quads's, Rectangles, and a possibility to rotate points, some results:
You can download the sketch here:
plotterLibExample.pde
For the ones working with the lib of Edwin Jakobs, here is his lib.
hpgltest.zip
Unzip this folder, and open and run the example, hpglTest.pde. The library sit in the tab next to it.
(it starts with the lissajou of the former post, but you can try out hitting the keys 0,1,2,3,4,5,6,7,8 and you get all kind of experimental stuff. Sometimes interactive, nearly alive. Feel free, or better, feel obliged to experiment!)
Aucun commentaire:
Enregistrer un commentaire