user2967353
user2967353

Reputation: 267

java math calculation for coordinates of circle given radius of 1.00

In one of my assignments, I am asked to write a program to calculate the (x, y) coordinates of points on a circle of radius 1.0. Display the output of y values for all x values ranging from 1.00 to negative 1.00 by increments of 0.1 and display the output neatly using printf, where all the x values are aligned vertically and to the right of all the x values, the y values are aligned vertically like:

 x1    y1
1.00  0.00
0.90  0.44

I know how to calculate the y values by using the Pythagorean theorem, but I don't know how to display every x and y values neatly by using a loop and formatting it with printf Below is my code that I have so far, any help will be greatly appreciated:

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

}

Upvotes: 1

Views: 5539

Answers (3)

Bussury
Bussury

Reputation: 56

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

The code within loop you can adjust them according to your need.

Upvotes: 3

Capn
Capn

Reputation: 21

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

}

Upvotes: 1

nhgrif
nhgrif

Reputation: 62052

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

That should get you started. If you don't understand the part within the parenthesis of the System.out.print statement, I suggest you look up what System.out.print does, look up format specifiers, and look up escape characters. Then you should be all set.

Upvotes: 0

Related Questions