-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIrregularPolygon.java
55 lines (46 loc) · 1.63 KB
/
IrregularPolygon.java
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
import java.awt.geom.*; // for Point2D.Double
import java.util.ArrayList; // for ArrayList
import gpdraw.*; // for DrawingTool
public class IrregularPolygon{
private ArrayList<Point2D.Double> myPolygon = new ArrayList<Point2D.Double>();
private DrawingTool pen = new DrawingTool(new SketchPad(500, 500));
// constructors
public IrregularPolygon() {
}
public void tester(){
add(20, 10);
add(70, 20);
add(50, 50);
add(0, 40);
}
// public methods
public void add(double aPoint, double bPoint) {
Point2D.Double Point = new Point2D.Double(aPoint, bPoint);
myPolygon.add(Point);
}
public void draw() {
pen.down();
for(int i = 0; i < myPolygon.size(); i++){
pen.move(myPolygon.get(i).getX(), myPolygon.get(i).getY());
pen.drawCircle(.5);
}
pen.move(myPolygon.get(0).getX(), myPolygon.get(0).getY());
}
public double perimeter() {
float Perimeter = 0;
for(int i = 0; i < myPolygon.size()-1; i++){
Perimeter += myPolygon.get(i).distance(myPolygon.get(i+1));
if(i + 2 == myPolygon.size()){
Perimeter += myPolygon.get(i+1).distance(myPolygon.get(0));
}
}
return Perimeter;
}
public double area() {
float Area = 0;
for(int i = 0; i < myPolygon.size(); i++){
Area += myPolygon.get(i).getX() * myPolygon.get(i + 1).getY() - myPolygon.get(i).getY() * myPolygon.get(i + 1).getX();
}
return Math.abs(Area / 2);
}
}