We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › "Wrapping up" multiple functions
Page Index Toggle Pages: 1
"Wrapping up" multiple functions (Read 632 times)
"Wrapping up" multiple functions
Apr 3rd, 2008, 8:51pm
 
I've written a small sketch which calculates the circumcircle for any given triangle. The code as it is works fine.

There are three custom functions. Into the first and second function go the x- and y-coordinates from the three corner-points of the triangle. These return the x and y for the circumcenter. Then I pass these into the third function to get the circumradius.

Code:

 // calculates the circumcircle
// april 3, 2008 | greg fraser

// click to redraw!

float x, y, r;
float u, v, d;

float x1, y1;
float x2, y2;
float x3, y3;

int big = 10;


void setup() {
 size(400,400);
 smooth();

 setThreePoints();

// here I call the functions which calculate the x, y and radius of the circumcircle
 x = circumX(x1,y1,x2,y2,x3,y3);
 y = circumY(x1,y1,x2,y2,x3,y3);
 r = circumR(x,y,x1,y1);

}

void draw() {
 background(0);

 // cornerpoints
 noStroke();
 fill(255);
 ellipse(x1,y1,big,big);
 ellipse(x2,y2,big,big);
 ellipse(x3,y3,big,big);
 
 // triangle
 stroke(255);
 line(x1,y1,x2,y2);
 line(x2,y2,x3,y3);
 line(x3,y3,x1,y1);
 
 // circumcircle
 noFill();
 stroke(255,0,0);
 ellipse(x,y,2*r,2*r);
 
}


float circumX(float _x1, float _y1, float _x2, float _y2, float _x3, float _y3) {
 u = (_y2-_y3)*(sq(_x1)+sq(_y1)) + (_y3-_y1)*(sq(_x2)+sq(_y2)) + (_y1-_y2)*(sq(_x3)+sq(_y3));
 d = _x1*_y2 + _x2*_y3 + _x3*_y1 - _x1*_y3 - _x2*_y1 - _x3*_y2;
 float tx = u/d*.5;
 return tx;
}

float circumY(float _x1, float _y1, float _x2, float _y2, float _x3, float _y3) {
 v = (_x3-_x2)*(sq(_x1)+sq(_y1)) + (_x1-_x3)*(sq(_x2)+sq(_y2)) + (_x2-_x1)*(sq(_x3)+sq(_y3));
 d = _x1*_y2 + _x2*_y3 + _x3*_y1 - _x1*_y3 - _x2*_y1 - _x3*_y2;
 float ty = v/d*.5;
 return ty;
}

float circumR(float _x, float _y, float _x1, float _y1) {
 float tr = sqrt(sq(_x-_x1)+sq(_y-_y1));
 return tr;
}

void mousePressed() {
 setThreePoints();

 x = circumX(x1,y1,x2,y2,x3,y3);
 y = circumY(x1,y1,x2,y2,x3,y3);
 r = circumR(x,y,x1,y1);
}

void setThreePoints() {
 // set three points
 x1 = random(width);
 y1 = random(height);
 x2 = random(width);
 y2 = random(height);
 x3 = random(width);
 y3 = random(height);
}



I feel this isn't the most elegant way to do this, since I create functions, get return values and put these into another custom function. I believe this should be possible to do in a single pass, send the cornerpoint-coordinates in, have the math do the magic, and then have x, y and r ready for read-out.

I'm imagining something like a circumcircle-calculator-object. Or even better, a package which, based on three coordinates, can calculate various useful values in and around the triangle. Anyone got a suggestion on where I could get started on this?

cheers.


Re: "Wrapping up" multiple functions
Reply #1 - Apr 12th, 2008, 8:58am
 
Try using a Class ;-)

The 3 points of your triangle and the circum position + radius would be parameters of your custom class.

Merge circumX, circumY and circumR into one method, say calculateCircum().

Pass the 3 points coordinates of your triangle to the constructor, and call calculateCircum().

Don't hesitate to ask for more info, if I'm not clear enough.
Re: "Wrapping up" multiple functions
Reply #2 - Apr 12th, 2008, 5:35pm
 
Yeah, this obviously how I should do it.

For some weird reason I was thinking that if I had a class which exists purely for computational reasons and doesn't have a visible representation in the sketch, I'd have to go at it in a different way.

One question remains. If I create a "TriangleCalculation" class and pass the three coordinates at object-creation, then calling calculateCircum() will only return the result for the initial coordinates. But when I create a new object but don't pass any values, I could have another method, say readCoordinates(), which I could use to dynamically feed new coordinates in and get the corresponding values. This way I could use the TriangleCalulation object multiple times in one pass. Is this correct? Does this even make sense?

Thanks.
Re: "Wrapping up" multiple functions
Reply #3 - Apr 13th, 2008, 1:37pm
 
Quote:
I was thinking that if I had a class which exists purely for computational reasons

This is not what I meant, actually. I was thinking of a custom Triangle class that you would instantiate for each triangle / computation you need. This would make each different triangle a new object.

Quote:
But when I create a new object but don't pass any values, I could have another method, say readCoordinates(), which I could use to dynamically feed new coordinates in and get the corresponding values.

Yes. You would probably call it setCoordinates() (set and get prefix are often used to set/get parameters).
Re: "Wrapping up" multiple functions
Reply #4 - Apr 13th, 2008, 3:08pm
 
antiplastik wrote on Apr 13th, 2008, 1:37pm:
This is not what I meant, actually. I was thinking of a custom Triangle class that you would instantiate for each triangle / computation you need. This would make each different triangle a new object.


Indeed, this was a misunderstanding on my part. Probably I should just start coding, instead of trying to sort everything out beforehand in my head. Thanks for the leads.
Re: "Wrapping up" multiple functions
Reply #5 - Apr 13th, 2008, 3:54pm
 
Ok, so here's what I've come up with. Does this conform to "elegant" programming rules?

Quote:


// calculates the circumcircle for any given triangle
// april 13, 2008 | greg fraser

// click to redraw!

Triangle t;

float x1, y1;
float x2, y2;
float x3, y3;

void setup() {
 size(400,400);
 smooth();
 newTriangle();
}

void draw() {
 background(0);

 t.render(); // draw triangle
 noFill();
 stroke(255,0,0);
 ellipse(t.getX(),t.getY(),2*t.getR(),2*t.getR());

}

void mousePressed() {  
 newTriangle();
}

void newTriangle() {
 t = new Triangle(random(width),random(height),random(width),random(height),random(width),random(height));
}


// Triangle class
class Triangle {

 // instance variables
 float x1,y1;
 float x2,y2;
 float x3,y3;
 float x, y, r, u, v, d;

 int dot = 6; // size of the corner dots

 // constructor
 Triangle(float x1, float y1, float x2, float y2, float x3, float y3) {
   this.x1 = x1;
   this.y1 = y1;
   this.x2 = x2;
   this.y2 = y2;
   this.x3 = x3;
   this.y3 = y3;

   calcCircum();
 }

 // methods
 void render() {

   // triangle
   stroke(255);
   line(x1,y1,x2,y2);
   line(x2,y2,x3,y3);
   line(x3,y3,x1,y1);

   // dots
   noStroke();
   fill(255);
   ellipse(x1,y1,dot,dot);
   ellipse(x2,y2,dot,dot);
   ellipse(x3,y3,dot,dot);
 }

 void calcCircum() {
   d = x1*y2 + x2*y3 + x3*y1 - x1*y3 - x2*y1 - x3*y2;

   u = (y2-y3)*(sq(x1)+sq(y1)) + (y3-y1)*(sq(x2)+sq(y2)) + (y1-y2)*(sq(x3)+sq(y3));    
   x = u/d*.5;

   v = (x3-x2)*(sq(x1)+sq(y1)) + (x1-x3)*(sq(x2)+sq(y2)) + (x2-x1)*(sq(x3)+sq(y3));
   y = v/d*.5;

   r = sqrt(sq(x-x1)+sq(y-y1));
 }

 float getX() {
   return x;
 }

 float getY() {
   return y;
 }

 float getR() {
   return r;
 }
}

Re: "Wrapping up" multiple functions
Reply #6 - Apr 14th, 2008, 12:54pm
 
Quote:
Does this conform to "elegant" programming rules?

Definitely. Good work ;-)
Re: "Wrapping up" multiple functions
Reply #7 - Apr 14th, 2008, 3:28pm
 
Awesome!

That's one point I can cross of my list of "How to become a better programmer". Just 99 left to go...
Page Index Toggle Pages: 1