-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDontTouch.java
More file actions
179 lines (145 loc) · 4.38 KB
/
DontTouch.java
File metadata and controls
179 lines (145 loc) · 4.38 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/**
* DontTouch.java
*
* Provide a description of the program here.
*
* @author YOUR NAME HERE
* @version 1.0
* @since 10/26/2022
*/
import java.awt.Color;
import java.awt.Font;
public class DontTouch
{
/** The array for the Circles to be drawn. */
Circle[] circles;
/** Constructs the size (1000) for the array of Circles. */
public DontTouch ( )
{
circles = new Circle[1000];
}
public static void main(String [] args)
{
DontTouch run = new DontTouch();
run.setUpCanvas();
run.drawCircles();
run.drawAxes();
run.printArea();
}
/**
* Sets up the canvas, using methods from StdDraw. This includes
* setting up the canvas size, the horizontal scale (Xscale), and
* the vertical scale (Yscale).
*/
public void setUpCanvas ( )
{
final int WIDTH = 1200;
final int HEIGHT = 800;
StdDraw.setCanvasSize(WIDTH, HEIGHT);
StdDraw.setXscale(-6, 6);
StdDraw.setYscale(-4, 4);
StdDraw.clear(new Color(255,255,255));
StdDraw.enableDoubleBuffering();
}
/**
* Creates the Circles in the array of Circles. Draws the Circles.
*/
public void drawCircles ( )
{
long startTime = System.currentTimeMillis();
// Your code here.
int circleCount = 0;
boolean works = false;
//double max = 2.0;
double rad = 1.0;
int count = 0;
for(int x = 0; x < 1000; x++) {
circles[x] = new Circle(0.0,0.0,0.0);
}
while(circleCount < 1000) {
for(int x = 0; x < 2000; x++) {
double newX = Math.random() * 13 - 6;
double newY = Math.random() * 9 - 4;
//System.out.println("NewX: " + newX);
//System.out.println("NewY: " + newY);
if(x >= circleCount) {
rad-= 0.000003;
x = 0;
}
if(circleWorks(newX, newY, rad, circleCount)) {
if(newX + rad < -6 || newX + rad > 6) {
System.out.println("Hey");
} else {
StdDraw.setPenColor(new Color((int)(255 * Math.random()), (int)(255 * Math.random()), (int)(255 * Math.random())));
circles[circleCount].setX(newX);
circles[circleCount].setY(newY);
circles[circleCount].setRadius(rad);
StdDraw.filledCircle(newX, newY, rad);
circleCount++;
break;
}
//System.out.println("Hey");
}
}
}
System.out.println(circleCount);
StdDraw.show();
long endTime = System.currentTimeMillis();
System.out.println("Time " + (endTime - startTime));
}
public boolean circleWorks(double x1, double y1, double r1, int circleCount) {
/*if ((x1+r1 >= 12) || (x1+r1 <=-12) || (y1+r1 >= 8) || (y1+r1 <=-8))
{
System.out.println("Touches end");
return false;
}*/
for(int x = 0; x < circleCount; x++) {
double distance = Math.sqrt(Math.abs((circles[x].getX()-x1)) * Math.abs((circles[x].getX()-x1)) + Math.abs((circles[x].getY() - y1)) * Math.abs(circles[x].getY() - y1));
//System.out.println(distance + " is distance.");
//System.out.println(circles[x].getRadius() + r1);
if(x1 + r1 >= 5.99999999999999 || x1 - r1 <= -5.999999999999) {
return false;
}
if(y1 + r1 >= 3.999999999999 || y1 - r1 <= -3.999999999999) {
return false;
}
if(distance <= circles[x].getRadius() + r1) {
return false;
}
}
return true;
}
// You may need to write a helper method or two.
/**
* Draws a pair of axes, over the drawn Circles. Grid lines are drawn and
* the scale is shown, to help the viewer see the size of the Circles.
*/
public void drawAxes ( )
{
Font font = new Font("Arial", Font.PLAIN, 18);
StdDraw.setFont(font);
StdDraw.setPenColor(new Color(220,220,220));
for(double integers = -6; integers <= 6; integers++)
{
StdDraw.line(integers,-4,integers,4);
StdDraw.line(-6,integers,6,integers);
StdDraw.setPenColor(new Color(0,0,0));
StdDraw.text(integers,-0.4,"" + (int)integers);
StdDraw.text(-0.3,integers-0.05,"" + (int)integers);
}
StdDraw.show();
}
/**
* Adds the area of each circle to a total area. Prints this total
* area to the terminal window.
*/
public void printArea ( )
{
double area = 0.0;
// Your code here.
for(int x = 0; x < 1000; x++) {
area = area + circles[x].getRadius() * circles[x].getRadius() * Math.PI;
}
System.out.println("\n\n\nTotal Area: " + area + "\n\n\n");
}
}