Visual creation of a triangle-filled circle
in
Programming Questions
•
2 months ago
Hello,
I have some troubles coding a sketch (using processing 2 with the Pshape library). Basically, what I want to do is a code that generate a visual pattern, something that looks a lot like the logo for the
creative applications logo: I want the shape of an ellipse to be filled with triangles, each of them with a different color, maybe even with radiants (one color per vertex).
The method I choose for this is to calculate the area of the ellipse, then try to calculate the area of every triangle I create, add them all until it sums up to the area of the ellipse.
So, here's my code:
- int num = 200; //just used to set up the arrays
- PShape s[] = new PShape[num]; //the triangle
- float x[] = new float[num];
- float y[] = new float[num]; //its coordinates
- float v_x[][] = new float[num][num];
- float v_y[][] = new float[num][num]; //the coordinates of every vertex that has been created this far
- float ray; //the radius of the circle
- float aire_cercle; //the area of the circle
- float aire_tri_totale; //the addition of each and every triangle's area
- float aire_tri; //the being-created triangle's area
- int t;
- int n_vertex;
- int i;
- void setup() {
- size(800, 600, P2D);
- frameRate(8); //I like things to go slow at first, so I can understand what's happening
- fill(80, 124, 132, 90);
- stroke(255);
- ray = 0.5*(height/1.2);
- background(0);
- //basic setup, nothing interesting here, I just set the radius of my circle
- }
- void draw() {
- translate(width/2, height/2); // I want things to start from the center of the screen
- aire_cercle = (ray*ray)*PI; //That's the area of my ellipse
- println(aire_cercle);
- //I want triangles to be created as long as their area do not match the ellipse's area
- while (aire_tri_totale < aire_cercle) {
- s[t] = createShape(); //we create a new shape
- beginShape();
- for (i = 0; i < 3; i++) { //i is the number of the vertex being created
- if (t == 0) { //if this is the first shape, she can start wherever she wants
- x[i] = random(-ray, ray);
- y[i] = random(-ray, ray);
- }
- else { //else, it will have to merge two of its three vertices to the previous shape
- if (i == 0) {
- x[i] = v_x[t-1][ceil(random(0, 1))]; //let's choose a vertex
- y[i] = v_y[t-1][ceil(random(0, 1))];
- }
- else if (i ==1) {
- x[i] = v_x[t-1][ceil(random(1, 2))]; //and another one
- y[i] = v_y[t-1][ceil(random(1, 2))];
- }
- else {
- //and finally, the third one is random within a certain range
- //(so that there ain't one big shape taking all of the place)
- x[i] = random(x[i-1]-ray/8, x[i-1]+ray/8);
- y[i] = random(y[i-1]-ray/8, y[i-1]+ray/8);
- }
- }
- v_x[t][i] = x[i];
- v_y[t][i] = y[i];
- //every vertice's coordinates gets to be stocked
- vertex(x[i], y[i]); //and is created
- }
- endShape(CLOSE);
- for (i = 2; i > 0; i--) {
- aire_tri += abs(((x[i]*y[i-1]) - (x[i-1]*y[i]))/2);
- /*
- I found this formula on a forum to calculate the area of a polygon. I can't really understand what
- it's doing though, moreover seeing if something wrong...
- As I recall, it is suppose to "go back" to each vertex, multiplying one of its coordinates with
- the other of the previous one, divising it all by 2...
- Nah, I can't even understand what I'm saying...
- */
- }
- aire_tri_totale += aire_tri;
- t++;
- println(aire_tri_totale);
- }
- noLoop();
- }
I have absolutely no idea what's going wrong, since the problem, I guess, lies in the part where the area is being calculated and that I can barely look without crying blood. Have I mentioned that I am a total failure when it comes to maths?
It even seems like the triangles a getting bigger every time one is created, which is totally unwanted, of course...
That's why I'm turning it to you, asking for help. As I am in a bit of a hurry, I would love to have someone giving me an hand on this.
Sorry if my english is a bit approximative, frenchman here, speaking.
Au revoir et merci d'avance!
Edit: I almost forgot, another issue, I would like vertices not to be created within a pre-existing triangle. Something like a hit-test system should do the trick, however I find myself incapable of creating, or at least, incorporate a thing like
that
1