This site requires JavaScript, please enable it in your browser!
Greenfoot back
ronald
ronald wrote ...

2023/10/12

Triangle around circle

ronald ronald

2023/10/12

#
Good morning I would like to know how to make triangles around a circle the triangle can be made in line or in polygon Is it necessary to calculate the center of the triangle in relation to ....... I don't know or to the center of the circle? or calculate the next point of the circle at an angle once the triangle is created at the top of the circle? Thank you for your help
ronald ronald

2023/10/13

#
I did some research I have the impression that it is more the right side of the triangle than the center of the circle that we must calculate the angle. on the other hand if the triangle does not touch the circle and there is a space between the two, how should it be done? THANKS
danpost danpost

2023/10/13

#
ronald wrote...
I did some research I have the impression that it is more the right side of the triangle than the center of the circle that we must calculate the angle. on the other hand if the triangle does not touch the circle and there is a space between the two, how should it be done? THANKS
Actually, the things you will need are the center-point, or origin, of the circle (x and y coordinates) and the radius of the circle. That is enough to draw an equilateral triangle around a circle, whether touching or not. Using drawPolygon is probably the best way. I came up with the following method:
private void drawTriangleAroundCircle(int centerX, int centerY, int radius, int gap) {
    radius += gap;
    GreenfootImage triangle = new GreenfootImage(radius*4, radius*4);
    triangle.setColor(Color.BLUE);
    int[] xs = new int[] { radius*2, radius*2-radius*173/100, radius*2+radius*173/100 };
    int[] ys = new int[] { 0, radius*3, radius*3 };
    triangle.drawPolygon(xs, ys, 3);
    getBackground()..drawImage(triangle, centerX-radius*2, centerY-radius*2);
}
The gap is how far off from touching you want to be. To be crossing the circle or be totally inside the circle, the gap can be made negative (as long as it does not negate the radius totally or more). The 173/100 is an approximation for the square root of 3.
ronald ronald

2023/10/13

#
thank you danpost so drawPolygon is better than drawLine, I also looked for how to make a for loop of 4 triangles with an angle or an arc, I didn't find much so I will base myself on your fracal recursion scenario without the depth so if I base myself on a rectangle which does not touch the circle or which touches the circle, for example, so I take the two sides of the rectangle closest to the circle by making a square root and I add an angle of 90 degrees for 4 rectangles let's imagine the rectangle in drawPolygon, to do an addition or a multiplication with two tables x and y, it seems difficult to me to make a square root and again I'm not sure that we can add an element of two tables we can find the length of the array x for example and I am unable to remember if we can add these two elements of an array or one element of each array x and y so I ask myself the question, I base myself on the rectangle or the triangle by adding an angle or an arc to make a for loop or only on the side which touches or does not touch the circle by thinking a little bit with your code that you gave me, I base myself on the radius of the two sides of the triangle near the circle where I take the whole triangle to make a for loop I have to see if I can make a square root with elements of a table or two tables, it seems skeptical to me, in any case, I don't remember anymore
danpost danpost

2023/10/14

#
ronald wrote...
I have to see if I can make a square root with elements of a table or two tables, it seems skeptical to me, in any case, I don't remember anymore
I think you misunderstand the purpose of the square root used in my code. An equilateral triangle is made up of two similar 30-60-90 triangles placed back to back with each other. The sides of which are in a 1-2-sqrt(3) ratio. That is why there is a square root of 3 in the code. A rectangle has opposite sides of equal length. No square root is required for that; though, that does not mean you can't have adjacent sides whose ratio is a square root.
ronald ronald

2023/10/14

#
it's true, I went a little too quickly, I didn't think enough, I'm 52 years old, I no longer work in the same way as at school, I have a worse memory, but hey I'll get through it. Your triangle isn't stupid, so I have to work with the adjacent side of the triangle, that's kind of what I wanted to know.
ronald ronald

2023/10/14

#
I'll come back to you danpost Good
for (double angle = 0; angle < 360; angle += 45) {
            double theta  = Math.toRadians(angle);
            int x = (int) ((radius * 3 + radius / 2) * Math.cos(theta) - (radius - centerY) * Math.sin(theta) + centerX);
            int y = (int) ((radius * 3 + radius / 2) * Math.sin(theta) + (radius - centerY) * Math.cos(theta) + centerY);
            circle.drawImage(triangle, x - 65, y + 125);
        }
I haven't found the right formula yet but I notice that the triangles enter the circle while I'm trying to make a little sun with 8 rays or 4 rays, in short, it doesn't matter the number of rays let's say that one side of the triangle must be say perpendicular to the circle I missed something, oops Thank you for your help
danpost danpost

2023/10/17

#
ronald wrote...
I'm trying to make a little sun with 8 rays or 4 rays, in short, it doesn't matter the number of rays let's say that one side of the triangle must be say perpendicular to the circle
One way to acquire the point locations is to add an actor into the world and move it around (start at center of circle, alter its rotation and move out, alternating between one radius and that plus length of ray). Create two arrays, one with x-coordinates and the other with y-coordinates, and add the points to them accordingly. Then, use fillPolygon to create the image. First, add a class for the actor:
public class Aktor extends greenfoot.Actor {}
That one line is the entire class code. The following creates and fills the arrays with the coordinates:
int centerX = 300, centerY = 300;
int radius = 100;
int rayCount = 8;
int rayLength = 50;
int[] xs = new int[rayCount*2];
int[] ys = new int[rayCount*2];
Actor point = new Aktor();
addObject(point, 0, 0);
for (int i=0; i<rayCount*2; i++) {
    point.setLocation(centerX, centerY);
    point.setRotation((i*360)/rayCount/2);
    point.move(radius+(1-i%2)*rayLength);
    xs[i] = point.getX();
    ys[i] = point.getY();
}
removeObject(point);
This can be followed by drawing the image:
GreenfootImage bg = getBackground();
bg.setColor(Color.YELLOW);
bg.fillPolygon(xs, ys, rayCount*2);
bg.setColor(Color.ORANGE);
bg.drawPolygon(xs, ys, rayCount*2);
ronald ronald

2023/10/17

#
thank you danpost and I added a good blue oval sometimes you don't have to look for super complicated calculation formulas then it's quite simple well thank you again
You need to login to post a reply.