-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNgon.java
More file actions
29 lines (25 loc) · 1.08 KB
/
Copy pathNgon.java
File metadata and controls
29 lines (25 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/* *****************************************************************************
* Name: Ada Lovelace
* Coursera User ID: 123456
* Last modified: October 16, 1842
**************************************************************************** */
public class Ngon {
public static void main(String[] args) {
int n = Integer.parseInt(args[0]);
double angle = 360.0 / n;
int canvasSize = Integer.parseInt(args[1]);
StdDraw.setCanvasSize(canvasSize, canvasSize);
StdDraw.setScale(0, canvasSize);
// actual calculation to find length of one side of an N-gon
double k = (n - 2) * 90.0 / n;
double step = 0.5 * canvasSize * (Math.sin(Math.toRadians(angle))) / Math.sin(
Math.toRadians(k));
//
// double step = canvasSize*Math.sin(Math.toRadians(angle / 2.0));//lecture provided value
Turtle turtle = new Turtle(0.5 * canvasSize, 0, angle / 2.0);
for (int i = 0; i < n; i++) {
turtle.goForward(step);
turtle.turnLeft(angle);
}
}
}