<< Chapter < Page Chapter >> Page >

As opposed to the curve() , in the bezier() function call the two control points specified by the four middle parameters are not points touchedby the curve. They only serve to define the shape of the approximating Bézier curve , which has the following interesting properties:

  • it is entirely contained in the convex hull defined by the extremal points and the control points;
  • transformations of translation, rotation, or scaling, appied to the extremal and controlpoints determine a similar transformation of the curve.
As we can see by running the code stroke(255, 0, 0); line(93, 40, 10, 10);line(90, 90, 15, 80); stroke(0, 0, 0);noFill(); bezier(93, 40, 10, 10, 90, 90, 15, 80); the control points lay on the tangent passing by the extremal points. In order to have an arbitrary number ofcontrol points one must use the bezierVertex() to specify each point within a block delimited by beginShape() and endShape() . In this way, an arbitrarily involute curve can be traced in the 3D space. In 2D, thefunction bezierVertex() has six parameters that correspond to the coordinates of two control points and oneanchor point. The first invocation of bezierVertex() has to be preceded by a call to vertex() which fixes the first anchor point of the curve.

There are other methods that allow to read the coordinates or the slope of the tangent to an arbitrary point of aBézier or spline curve. Such point can be specified by a parameter t that can go from 0 (first extreme) to 1 (second extreme). It is also possible to set the precision of approximating orinterpolating curves in 3D. For details see the Processing reference manual .

The Processing sketch in table shows the difference between the spline interpolating curve and the Bézier curve.

See the term Bézier curve in Wikipedia.
applet that compares the Bézier curve (red) and theinterpolating spline (black) void setup() { c1x = 120;c1y = 110; c2x = 50;c2y = 70; background(200);stroke(0,0,0); size(200, 200);} int D1, D2;int X, Y; int c1x, c1y, c2x, c2y;void draw() { if (mousePressed == true) {X = mouseX; Y = mouseY; // selection of the point that is modifiedD1 = (X - c1x)*(X - c1x) + (Y - c1y)*(Y - c1y); D2 = (X - c2x)*(X - c2x) + (Y - c2y)*(Y - c2y);if (D1<D2) { c1x = X; c1y = Y;} else {c2x = X; c2y = Y; }} background(200);stroke(0,0,0); strokeWeight(1);noFill();beginShape(); curveVertex(10, 10);curveVertex(10, 10); curveVertex(c2x, c2y);curveVertex(c1x, c1y); curveVertex(190, 190);curveVertex(190, 190); endShape();stroke(255,30,0);bezier(10,10,c2x,c2y,c1x,c1y,190,190); strokeWeight(4);point(c1x,c1y); point(c2x,c2y);}

2d

Objects in two or three dimensions take a color that can be determined by the illumination, asexplained in [link] , or established by the method fill() , which also gives the possibility to set the degree of transparency.

Triangles

The triangle is the fundamental construction element for 3D graphics. In fact,by juxtaposition of triangles one can approximate any continuous surface. In Processing, however, the trianglesare specified in 2D by the primitive triangle() , whose six parameters correspond to the coordinates of thevertices in the image window. Even though each triangle is defined in 2D, it can be rotated and translated in the 3Dspace, as it happens in the Processing sketch void setup(){ size(200, 200, P3D); fill(210, 20, 20); }float angle = 0; void draw(){background(200); // clear image stroke(0,0,0);angle += 0.005; rotateX(angle);triangle(10,10,30,70,80,80); }

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Media processing in processing. OpenStax CNX. Nov 10, 2010 Download for free at http://cnx.org/content/col10268/1.14
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Media processing in processing' conversation and receive update notifications?

Ask