如何找到围绕另一个圆周旋转的圆周长坐标?

时间:2017-02-06 23:41:49

标签: java user-interface geometry trigonometry

public static void drawSpiral(Display panel) {
    int centerX = panel.getWidth() / 2;
    int centerY = panel.getHeight() / 2;

    double degAng = 270;
    double radius = 150;
    double x, y, radAng;
    while ( true ) {

        radAng = ( degAng * Math.PI ) / 180;
        x = centerX + radius * Math.cos ( radAng );
        y = centerY + radius * Math.sin ( radAng );

        panel.drawNextPoint ( (int) x, (int) y );
        degAng += 0.45;
    }
}

我试图创建一个使用绘制的简单GUI的方法。上面的方法从顶部开始绘制半径为150的简单圆的坐标。我试图绘制一个圆圈,使用此方法给出的点作为其中心点。

这是我最近尝试过的,它只给了我一个椭圆!

public static void drawCircle(Display panel) {
    int centerX = panel.getWidth() / 2;
    int centerY = panel.getHeight() / 2;

    double degAng = 270;
    double newDegAng = 0;
    double newRadius = 25;
    double radius = 150;
    double x, y, radAng, newX, newY, newRadAng;
    while ( true ) {

        radAng = ( degAng * Math.PI ) / 180;
        x = centerX + radius * Math.cos ( radAng );
        y = centerY + radius * Math.sin ( radAng );

        newRadAng = (newDegAng * Math.PI) / 180;
        newX =  x - newRadius * Math.cos (newRadAng);
        newY =  y - newRadius * Math.sin (newRadAng);

        panel.drawNextPoint ( (int) newX, (int) newY );
        degAng += 0.45;
        newDegAng -= 0.45;
    }
}

1 个答案:

答案 0 :(得分:2)

在你到达下一个点之前,你需要让你的圈子用尽。所以你需要一个嵌套循环:

double degAng = 0;
double newDegAng = 0;
double newRadius = 25;
double radius = 150;
double x, y, radAng, newX, newY, newRadAng;
while ( degAng<360 ) {
    radAng = ( degAng * Math.PI ) / 180;
    x = centerX + radius * Math.cos ( radAng );
    y = centerY + radius * Math.sin ( radAng );

    newDegAng = 0;
    while ( newDegAng>-360 ) {
      newRadAng = (newDegAng * Math.PI) / 180;
      newX =  x - newRadius * Math.cos (newRadAng);
      newY =  y - newRadius * Math.sin (newRadAng);
      drawPoint ( (int) newX, (int) newY);
      newDegAng -= 0.45;
    }

    drawPoint( (int) x, (int) y );
    degAng += 0.45;
}

这会创建一个像这样的好照片

enter image description here

这会绘制所有圆圈,因此可以有效地创建一个圆管。