给定半径为1.00的圆坐标的java数学计算

时间:2013-11-11 19:04:54

标签: java math geometry

在我的一项任务中,我被要求编写一个程序来计算半径为1.0的圆上的点的(x,y)坐标。显示所有x值的y值输出,范围从1.00到负1.00,增量为0.1,并使用printf整齐地显示输出,其中所有x值垂直对齐并且在所有x值的右侧, y值垂直对齐,如:

 x1    y1
1.00  0.00
0.90  0.44

我知道如何使用毕达哥拉斯定理来计算y值,但我不知道如何通过使用循环并用printf格式化它来整齐地显示每个x和y值。下面是我的代码到目前为止,我们将非常感谢任何帮助:

public class PointsOnACircleV1 {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here

    // // create menu

    // create title
    System.out.println("Points on a circle of Radius 1.0");

    // create x1 and y1
    System.out.println("          x1                         y1");

    // create line
    System.out.println("_________________________________________________");

    // // display x values

    // loop?


    // // perform calculation

    // radius
    double radius = 1.00;

    // x value
    double x = 1.00;

    // calculate y value
    double y = Math.pow(radius, 2) - Math.pow(x, 2);
}

}

3 个答案:

答案 0 :(得分:3)

public static void main(String[] args) {

    double radius =  1.00;
    double x  , y ;

    for ( x=-1.0 ; x<=1.0; x+=0.2 ) {
        y = Math.sqrt(radius - Math.pow(x,2)) ;
        System.out.printf("\n" + x +"     "+ y);
    }
}

循环中的代码可以根据需要进行调整。

答案 1 :(得分:1)

 public class PointsOnACircleV1
 {
  public static void main (String [] args)
{
    double r = 1; //radius initialized to one

    double x = 1; // x coordinate initialized to one, could be anything
    double y = 0.0; // y coordinate is dependent so left at 0.

    //output
    System.out.println("\tPoints on a Circle of Radius 1.0"); 
    System.out.printf("\t%6s%6s%12s%7s\n", "x1", "y1", "x1", "y2");
    System.out.println("--------------------------------------------");

    //for loop to decrement values from the initialized x coordinate to the 
    //end of the diameter, radius is 1 so diameter is 2 so 1 to -1.
    for(x = 1; x >= -1; x -= .1)
    {
        y = Math.sqrt(Math.pow(r,2) - Math.pow(x,2)); //pythagorean theorem to achieve y value.
        System.out.printf("\t%6.2f%7.2f%12.2f%8.2f\n", x, y, x, -y); //output, -y to get values
        //for the other 1/2 of the circle
    }
}

}

答案 2 :(得分:0)

for(int i=100; i>=-100; i-=10) {
    x = i/100.0;
    //do stuff
    System.out.print("\t%.2f\t%.2f", x, y);
}

这应该让你开始。如果您不理解System.out.print语句的括号内的部分,建议您查找System.out.print的内容,查找format specifiers,然后查找escape characters。那么你应该全力以赴。