Progo
Progo

Reputation: 3490

Java draw triangle

I have been using the following code to draw a rotated polygon:

public static void renderPoly(Graphics g, double cx, double cy, int sides, double radius, double rotation){
    Graphics2D g2d = (Graphics2D) g;
    double deltaAngle = (Math.PI * 2) / sides;
    double angle;
    double lastX = cx + radius * Math.cos(0 - rotation);
    double lastY = cy + radius * Math.sin(0 - rotation);
    for(int n = 1; n <= sides; n++){
        angle = deltaAngle * n;
        double x = cx + radius * Math.cos(angle - rotation);
        double y = cx + radius * Math.cos(angle - rotation);
        g2d.drawLine((int)lastX, (int)lastY, (int)x, (int)y);
        lastX = x;
        lastY = y;
    };
};

Unfortunately, this is failing to work for me when I call it:

renderPoly(g, 15, 15, 3, 5, Math.toRadians(-90));

enter image description here

What has gone wrong in my code? Thank you.

Upvotes: 0

Views: 290

Answers (1)

ControlAltDel
ControlAltDel

Reputation: 35011

I think this is wrong

        double y = cx + radius * Math.cos(angle - rotation);

should be sin, no?

Upvotes: 1

Related Questions