Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.geom.*;
- import java.util.ArrayList;
- import gpdraw.*;
- public class IrregularPolygonAZ
- {
- private ArrayList <Point2D.Double> myPolygon;
- private DrawingTool myPencil;
- private SketchPad myPaper;
- /**
- * Constructor for objects of class IrregularPolygonAZ
- */
- public IrregularPolygonAZ()
- {
- myPolygon = new ArrayList <Point2D.Double> ();
- myPaper = new SketchPad(500, 500);
- myPencil = new DrawingTool(myPaper);
- }
- public void add(Point2D.Double aPoint)
- {
- myPolygon.add(aPoint);
- }
- public void draw()
- {
- myPencil.up();
- myPencil.move(myPolygon.get(0).x, myPolygon.get(0).y );
- myPencil.down();
- for(int x = 1; x < myPolygon.size(); x++)
- {
- myPencil.move(myPolygon.get(x).x, myPolygon.get(x).y);
- }
- myPencil.move(myPolygon.get(0).x, myPolygon.get(0).y);
- }
- public double perimeter()
- {
- double total = 0;
- int cnt;
- for(cnt = 0; cnt < myPolygon.size() - 1; cnt++)
- {
- total += Math.sqrt( Math.pow((myPolygon.get(cnt + 1).x - myPolygon.get(cnt).x), 2) + Math.pow((myPolygon.get(cnt + 1).y - myPolygon.get(cnt).y), 2));
- }
- total += Math.sqrt( Math.pow((myPolygon.get(0).x - myPolygon.get(cnt).x), 2) + Math.pow((myPolygon.get(0).y - myPolygon.get(cnt).y), 2));
- return total;
- }
- public double area()
- {
- double total = 0;
- for(int ct = 0; ct < myPolygon.size() - 1; ct++)
- {
- total += ( (myPolygon.get(ct).x * myPolygon.get(ct+1).y) - (myPolygon.get(ct).y * myPolygon.get(ct+1).x) );
- }
- total = total / 2.0;
- return total;
- }
- public double distance(int c, int v)
- {
- double value = 0.0;
- value = Math.sqrt( Math.pow((myPolygon.get(v).x - myPolygon.get(c).x), 2) + Math.pow((myPolygon.get(v).y - myPolygon.get(c).y), 2));
- return value;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement