Recently I've been looking at Povray, pyprocessing, and cfdg (version 3.0) as tools for creating digital images. I have branched two separate blogs where I mainly explore jruby + processing and processing.py

Sunday, 16 November 2008

Refactored Hybrid Shape

I can seriously recommend using netbeans to develop your processing code, this is my second refactored hybrid shape (see page 367 of the book Figure 9-20), in the first instance I 'functionalised' the code, in this second pass I have introduced a class. In both cases netbeans made the refactoring a cinch and its a doddle to convert the java code back to pde format.

Here is my refactored Gear class, another change I made was cut out the redundant conversion from degrees to radians, I believe the reader should get into radians rather have all the unnecessary and messy conversions (it would need to be introduced earlier in the book):

   1:import processing.core.*;
2:
3:void setup() {
4: size(600, 600);
5: strokeWeight(10);
6: int teeth = 8;
7: float rad1 = 250.0f;
8: fill(255);
9: Gear gear = new Gear(teeth, width / 2, height / 2, rad1);
10: gear.drawGear();
11:}
12:
13:class Gear {
14:
15: int cogs = 8;
16: int x = 0, y = 0;
17: float rad = 10;
18:
19: /**
20: * Constructor with arguments
21: */
22: Gear(int teeth, int xpos, int ypos, float radius) {
23: cogs = teeth;
24: x = xpos;
25: y = ypos;
26: rad = radius;
27: }
28:
29: /**
30: * Default Constructor
31: */
32: Gear() {
33: }
34:
35: /**
36: * creates a hybrid shape with
37: * @param sides int number of sides
38: * @param x int center position
39: * @param y int center position
40: * @param rad1 float radius curve
41: */
42: private void makeHybrid(int sides, int x, int y, float rad1) {
43: curveTightness(-0.8f);
44: smooth();
45: beginShape();
46: float[] cxy = new float[2];
47: float[] pxy = new float[2];
48: float angle = PI/sides;
49: float ang = TWO_PI / (sides + sides / 2.0f);
50: float ang2 = ang / 2.0f;
51: float rad2 = rad1 * 2.0f;
52: for (int i = 0; i < sides; i++) {
53: cxy = shapePosition(x, y, angle, rad2);
54: curveVertex(cxy[0], cxy[1]);
55: pxy = shapePosition(x, y, angle, rad1);
56: curveVertex(pxy[0], pxy[1]);
57: angle += ang;
58: pxy = shapePosition(x, y, angle, rad1);
59: curveVertex(pxy[0], pxy[1]);
60: cxy = shapePosition(x, y, angle, rad2);
61: curveVertex(cxy[0], cxy[1]);
62: pxy = shapePosition(x, y, angle, rad1);
63: vertex(pxy[0], pxy[1]);
64: angle += ang2;
65: pxy = shapePosition(x, y, angle, rad1);
66: vertex(pxy[0], pxy[1]);
67: }
68: endShape(CLOSE);
69: }
70:
71: /**
72: * Calculate shape coordinates from radius, angle and center coordinates
73: * and the angle and radius parameters
74: * @param x int center position
75: * @param y int center position
76: * @param angle radians
77: * @param radius float
78: * @return {xnew, ynew} float[]
79: */
80: private float[] shapePosition(int x, int y, float angle, float radius) {
81: x += cos(angle) * radius;
82: y += sin(angle) * radius;
83: float[] result = new float[2];
84: result[0] = x;
85: result[1] = y;
86: return result;
87: }
88:
89: /**
90: * public draw method
91: */
92: void drawGear() {
93: makeHybrid(cogs, x, y, rad);
94: noLoop();
95: }
96:}



For some reason both the original code and my refactored code produce extraneous bits off the gear outline, does this happen for anyone else?

No comments:

Post a Comment

Followers

Blog Archive

About Me

My photo
Pembrokeshire, United Kingdom
I have developed JRubyArt and propane new versions of ruby-processing for JRuby-9.1.5.0 and processing-3.2.2