Java绘制三角形

时间:2014-09-24 01:58:40

标签: java rendering render draw

我一直在使用以下代码绘制旋转多边形:

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;
    };
};

不幸的是,当我打电话时,这对我没有用:

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

enter image description here

我的代码出了什么问题?谢谢。

1 个答案:

答案 0 :(得分:1)

我认为这是错误的

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

应该是罪,不是吗?