Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Point {
- private double x, y;
- public Point(double xx, double yy) {
- x = xx;
- y = yy;
- }
- public double getX() {
- return x;
- }
- public double getY() {
- return y;
- }
- public double getDistance(Point point) {
- double distance = Math.sqrt(Math.pow(x - point.getX(), 2) + Math.pow(y - point.getY(), 2));
- return distance;
- }
- }
- abstract class Shape {
- public abstract double getArea();
- public abstract double getPerimeter();
- }
- interface Symmetric {
- public Point getPointOfSymmetry();
- }
- class Triangle extends Shape {
- Point firstPoint, secondPoint, thirdPoint;
- public Triangle(Point fPoint, Point sPoint, Point tPoint) {
- firstPoint = fPoint;
- secondPoint = sPoint;
- thirdPoint = tPoint;
- }
- public Point getFirstPoint() {
- return firstPoint;
- }
- public Point getSecondPoint() {
- return secondPoint;
- }
- public Point getThirdPoint() {
- return thirdPoint;
- }
- @Override
- public double getArea() {
- // area = abs((Ax(By-Cy) + Bx(Cy-Ay) + Cx(Ay-By))/2)
- // https://www.mathopenref.com/coordtrianglearea.html
- double area = Math.abs((firstPoint.getX() * (secondPoint.getY() - thirdPoint.getY()) +
- secondPoint.getX() * (thirdPoint.getY() - firstPoint.getY()) +
- thirdPoint.getX() * (firstPoint.getY() - secondPoint.getY())) / 2);
- return area;
- }
- @Override
- public double getPerimeter() {
- double firstToSecondPoint = firstPoint.getDistance(secondPoint);
- double secondToThirdPoint = secondPoint.getDistance(thirdPoint);
- double thirdToFirstPoint = thirdPoint.getDistance(firstPoint);
- return firstToSecondPoint + secondToThirdPoint + thirdToFirstPoint;
- }
- }
- class Rectangle extends Shape {
- Point topLeftPoint;
- double length, width;
- public Rectangle(Point tLeftPoint, double l, double w) {
- topLeftPoint = tLeftPoint;
- length = l;
- width = w;
- }
- public Point getTopLeftPoint() {
- return topLeftPoint;
- }
- public double getLength() {
- return length;
- }
- public double getWidth() {
- return width;
- }
- @Override
- public double getArea() {
- return length * width;
- }
- @Override
- public double getPerimeter() {
- return 2 * (width + length);
- }
- }
- class Circle extends Shape implements Symmetric {
- Point center;
- double radius;
- public Circle(Point c, double r) {
- center = c;
- radius = r;
- }
- public Point getCenter() {
- return center;
- }
- public double getRadius() {
- return radius;
- }
- @Override
- public double getArea() {
- return Math.PI * radius * radius;
- }
- @Override
- public double getPerimeter() {
- return 2 * Math.PI * radius;
- }
- @Override
- public Point getPointOfSymmetry() {
- //return the center
- return center;
- }
- }
- class Trapezoid extends Shape {
- Point topLeftPoint, bottomLeftPoint;
- double topSide, bottomSide;
- public Trapezoid(Point tlp, Point blp, double ts, double bs) {
- topLeftPoint = tlp;
- bottomLeftPoint = blp;
- topSide = ts;
- bottomSide = bs;
- }
- public Point getTopLeftPoint() {
- return topLeftPoint;
- }
- public Point getBottomLeftPoint() {
- return bottomLeftPoint;
- }
- public double getTopSide() {
- return topSide;
- }
- public double getBottomSide() {
- return bottomSide;
- }
- @Override
- public double getArea() {
- // lets determine the point in bottom line to get height
- Point aPoint = new Point(topLeftPoint.getX(), bottomLeftPoint.getY());
- double height = topLeftPoint.getDistance(aPoint);
- double area = height * (topSide + bottomSide) / 2;
- return area;
- }
- @Override
- public double getPerimeter() {
- double leftTopToBottomTop = topLeftPoint.getDistance(bottomLeftPoint);
- Point rightTopSide = new Point(topLeftPoint.getX() + topSide, topLeftPoint.getY());
- Point rightBottomSide = new Point(bottomLeftPoint.getX() + bottomSide, bottomLeftPoint.getY());
- double rightTopToBottomTop = rightTopSide.getDistance(rightBottomSide);
- return leftTopToBottomTop + rightTopToBottomTop + topSide + bottomSide;
- }
- }
- class EquilateralTriangle extends Triangle implements Symmetric{
- double side;
- public EquilateralTriangle(Point topPoint, double s){
- super(topPoint,new Point(topPoint.getX(),topPoint.getY()-s),new Point(topPoint.getX()+s,topPoint.getY()-s));
- side = s;
- }
- public Point getTopPoint(){
- return firstPoint;
- }
- public double getSide(){
- return side;
- }
- @Override
- public Point getPointOfSymmetry(){
- Point midPointBase = new Point(secondPoint.getX() + side/2, secondPoint.getY());
- double distanceFromMidToTopPoint = midPointBase.getDistance(firstPoint);
- Point symPoint = new Point(midPointBase.getX(),midPointBase.getY() - distanceFromMidToTopPoint/3);
- return symPoint;
- }
- }
- class Square extends Rectangle implements Symmetric {
- public Square(Point topLeft, double side) {
- super(topLeft, side, side);
- }
- public double getSide() {
- return super.getLength();
- }
- @Override
- public Point getPointOfSymmetry() {
- //This should return the centre
- double symX = topLeftPoint.getX() + width / 2;
- double symY = topLeftPoint.getY() - width / 2;
- Point p = new Point(symX, symY);
- return p;
- }
- }
- class Plane {
- private Shape[] shapes = new Shapes[0];
- public Plane() {}
- private int getShapeCounter() {
- return shapes.length;
- }
- public Shape[] getShape() {
- return shapes;
- }
- public void addShape(Shape shape) {
- Shape[] newShapes = new Shape[shapes.length + 1];
- for (int i = 0; i < shapes.length; i++)
- newShapes[i] = shapes[i];
- newShapes[shapeList.length] = shape;
- shapes = newShapes;
- }
- public double getSumOfAreas() {
- double totalArea = 0;
- for (int i = 0; i < getShapeCounter(); i++) {
- totalArea += shapes[i].getArea();
- }
- return totalArea;
- }
- public double getSumOfPerimeters() {
- double totalPerimeter = 0;
- for (int i = 0; i < getShapeCounter(); i++) {
- totalPerimeter += shapes[i].getArea();
- }
- return totalPerimeter;
- }
- public Point getCenterOfPointOfSymmetries() {
- double xSym = 0;
- double ySym = 0;
- int count = 0;
- for (int i = 0; i < getShapeCounter(); i++) {
- if (shapes[i].getClass() == EquilateralTriangle.class) {
- EquilateralTriangle et = (EquilateralTriangle) shapes[i];
- xSym += et.getPointOfSymmetry().getX();
- ySym += et.getPointOfSymmetry().getY();
- count++;
- } else if (shapes[i].getClass() == Circle.class) {
- Circle c = (Circle) shapes[i];
- xSym += c.getPointOfSymmetry().getX();
- ySym += c.getPointOfSymmetry().getY();
- count++;
- } else if (shapes[i].getClass() == Square.class) {
- Square s = (Square) shapes[i];
- xSym += s.getPointOfSymmetry().getX();
- ySym += s.getPointOfSymmetry().getY();
- count++;
- }
- }
- // Get the average
- xSym = xSym / count;
- ySym = ySym / count;
- Point p = new Point(xSym, ySym);
- return p;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement