Advertisement
Egonau

SHapes

Apr 7th, 2023
437
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.99 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. abstract class Shape{
  4.     public abstract Point center();
  5.     public abstract double perimeter();
  6.     public abstract double area();
  7.     public abstract void translate(final Point newCenter);
  8.     public abstract void rotate(final double angle);
  9.     public abstract void scale(final double coefficient);
  10. }
  11. class Point{
  12.     double x,y;
  13.  
  14.     public Point(double x, double y) {
  15.         this.x = x;
  16.         this.y = y;
  17.     }
  18.  
  19.     public Point add(double x,double y){
  20.         return new Point(this.x+x,this.y+y);
  21.     }
  22.     public Point subtract(double x,double y){
  23.         return new Point(this.x-x,this.y-y);
  24.     }
  25.     public Point multiply(double factor){
  26.         return new Point(this.x*factor,this.y*factor);
  27.     }
  28.     public Point divide(double factor){
  29.         return new Point(this.x/factor,this.y/factor);
  30.     }
  31.     public Point rotate(Point p, double angle){
  32.         double sin = Math.sin(angle);
  33.         double cos = Math.cos(angle);
  34.         double rotatedX = p.x + (x-p.x)*cos-(y-p.y)*sin;
  35.         double rotatedY = p.y + (x-p.x)*sin+(y-p.y)*cos;
  36.         return new Point(rotatedX,rotatedY);
  37.     }
  38.     public double x(){
  39.         return x;
  40.     }
  41.     public double y(){
  42.         return y;
  43.     }
  44.     public Point midpoint(Point p){
  45.         return new Point((p.x+x)/2,(p.y+y)/2);
  46.     }
  47.     public static double distance(Point p,Point p1){
  48.         return Math.sqrt((p.x-p1.x)*(p.x-p1.x)+(p.y-p1.y)*(p.y-p1.y));
  49.     }
  50. }
  51. class Ellipse extends Shape{
  52.     Point p1,p2;
  53.     double perifocus;
  54.     public Ellipse(Point p1,Point p2,double perifocus) {
  55.         this.p1 = p1;
  56.         this.p2 = p2;
  57.         this.perifocus = perifocus;
  58.     }
  59.     public List<Point> focuses(){
  60.         List<Point> l = new ArrayList<>();
  61.         l.add(p1);
  62.         l.add(p2);
  63.         return l;
  64.     }
  65.  
  66.     public double focalDistance(){
  67.         return Point.distance(p1,p2)/2;
  68.     }
  69.  
  70.     public double majorSemiAxis(){
  71.         return Point.distance(p1,this.center())+perifocus;
  72.     }
  73.  
  74.     public double minorSemiAxis(){
  75.         double maxx = Math.max(Math.pow(this.majorSemiAxis(),2),Math.pow(Point.distance(p1,this.center()),2));
  76.         double minn =  Math.min(Math.pow(this.majorSemiAxis(),2),Math.pow(Point.distance(p1,this.center()),2));
  77.         return Math.sqrt(maxx-minn);
  78.     }
  79.  
  80.     public double eccentricity(){
  81.         return Point.distance(p1,this.center())/this.majorSemiAxis();
  82.     }
  83.     @Override
  84.     public Point center() {
  85.         return p1.midpoint(p2);
  86.     }
  87.  
  88.     @Override
  89.     public double perimeter() {
  90.         double a = this.majorSemiAxis();
  91.         double b = this.minorSemiAxis();
  92.         return (4*((Math.PI*a*b+(a-b)*(a-b))/(a+b)));
  93.     }
  94.  
  95.     @Override
  96.     public double area() {
  97.         double a = this.majorSemiAxis();
  98.         double b = this.minorSemiAxis();
  99.         return Math.PI*a*b;
  100.     }
  101.  
  102.     @Override
  103.     public void translate(Point newCenter) {
  104.         Point currentCenter = center();
  105.         double dX = newCenter.x-currentCenter.x;
  106.         double dY = newCenter.y-currentCenter.y;
  107.         p1 = p1.add(dX,dY);
  108.         p2 = p2.add(dX,dY);
  109.     }
  110.  
  111.     @Override
  112.     public void rotate(double angle) {
  113.         Point center = center();
  114.         p1 = p1.rotate(center,angle);
  115.         p2 = p2.rotate(center,angle);
  116.     }
  117.  
  118.     @Override
  119.     public void scale(double coefficient) {
  120.         perifocus=Math.abs(perifocus*coefficient);
  121.         Point center = center();
  122.         double dx1 = p1.x - center.x;
  123.         double dy1 = p1.y - center.y;
  124.         double dx2 = p2.x - center.x;
  125.         double dy2 = p2.y - center.y;
  126.         p1 = new Point(center.x + dx1 * coefficient, center.y + dy1 * coefficient);
  127.         p2 = new Point(center.x + dx2 * coefficient, center.y + dy2 * coefficient);
  128.     }
  129. }
  130. class Circle extends Ellipse{
  131.     Point p;
  132.     double perifocus;
  133.     public Circle(Point p, double perifocus) {
  134.         super(p, p, perifocus);
  135.         this.p = p;
  136.         this.perifocus  = perifocus;
  137.     }
  138.     public double radius(){
  139.         return minorSemiAxis();
  140.     }
  141. }
  142. class Rectangle extends Shape{
  143.     Point p1,p2;
  144.     double height;
  145.     public Rectangle(Point p1, Point p2, double height){
  146.         this.p1 = p1;
  147.         this.p2 = p2;
  148.         this.height = height;
  149.     }
  150.     public List<Point> vertices() {
  151.         Point center = center();
  152.         double angle = Math.atan2(Math.abs(center.y-p1.y),Math.abs(center.x-p1.x));
  153.         List<Point> vertices = new ArrayList<>();
  154.         double halfWidth = firstSide() / 2.0;
  155.         double halfHeight = secondSide() / 2.0;
  156.         double sin = Math.sin(angle);
  157.         double cos = Math.cos(angle);
  158.         Point vertex1 = new Point(center.x + cos * (-halfWidth) - sin * (-halfHeight),
  159.                 center.y+ sin * (-halfWidth) + cos * (-halfHeight));
  160.         Point vertex2 = new Point(center.x + cos * (halfWidth) - sin * (-halfHeight),
  161.                 center.y + sin * (halfWidth) + cos * (-halfHeight));
  162.         Point vertex3 = new Point(center.x + cos * (halfWidth) - sin * (halfHeight),
  163.                 center.y + sin * (halfWidth) + cos * (halfHeight));
  164.         Point vertex4 = new Point(center.x + cos * (-halfWidth) - sin * (halfHeight),
  165.                 center.y + sin * (-halfWidth) + cos * (halfHeight));
  166.         vertices.add(vertex1);
  167.         vertices.add(vertex2);
  168.         vertices.add(vertex3);
  169.         vertices.add(vertex4);
  170.         vertices.sort((pA, pB) -> {
  171.             double angle1 = Math.atan2(pA.y - center.y, pA.x - center.x);
  172.             double angle2 = Math.atan2(pB.y - center.y, pB.x - center.x);
  173.             if (angle1 < 0) {
  174.                 angle1 += 2 * Math.PI;
  175.             }
  176.             if (angle2 < 0) {
  177.                 angle2 += 2 * Math.PI;
  178.             }
  179.             return Double.compare(angle1, angle2);
  180.         });
  181.         List<Double> l = new ArrayList<>();
  182.         double ang = Math.atan2(p1.y-center.y,p1.x-center.x);
  183.         if (ang < 0) {
  184.             ang += 2 * Math.PI;
  185.         }
  186.         for (Point i : vertices){
  187.             double an = Math.atan2(i.y-center.y,i.x-center.x);
  188.             if (an < 0) {
  189.                 an += 2 * Math.PI;
  190.             }
  191.             l.add(an);
  192.         }
  193.  
  194.         if (ang > l.get(3)){
  195.             return vertices;
  196.         }
  197.         else{
  198.             int pointer = 0;
  199.             for (int i = 0;i<3;++i){
  200.                 if (ang<l.get(i)){
  201.                     pointer = i;
  202.                     break;
  203.                 }
  204.             }
  205.             List<Point> p = new ArrayList<>();
  206.             for (int i = pointer;i<l.size();++i){
  207.                 p.add(vertices.get(i));
  208.             }
  209.             for (int i = 0;i<pointer;++i){
  210.                 p.add(vertices.get(i));
  211.             }
  212.             return p;
  213.         }
  214.     }
  215.  
  216.  
  217.     public double firstSide(){
  218.         return Point.distance(p1,p2);
  219.     }
  220.     public double secondSide(){
  221.         return height;
  222.     }
  223.     public double diagonal(){
  224.         return Math.sqrt(firstSide()*firstSide()+secondSide()*secondSide());
  225.     }
  226.  
  227.     @Override
  228.     public Point center() {
  229.         return p1.midpoint(p2);
  230.     }
  231.  
  232.     @Override
  233.     public double perimeter() {
  234.         return (firstSide()+secondSide())*2;
  235.     }
  236.  
  237.     @Override
  238.     public double area() {
  239.         return firstSide()*secondSide();
  240.     }
  241.  
  242.     @Override
  243.     public void translate(Point newCenter) {
  244.         Point currentCenter = center();
  245.         double dX = newCenter.x-currentCenter.x;
  246.         double dY = newCenter.y-currentCenter.y;
  247.         p1 = p1.add(dX,dY);
  248.         p2 = p2.add(dX,dY);
  249.     }
  250.  
  251.     @Override
  252.     public void rotate(double angle) {
  253.         Point center = center();
  254.         p1 = p1.rotate(center,angle);
  255.         p2 = p2.rotate(center,angle);
  256.     }
  257.  
  258.     @Override
  259.     public void scale(double coefficient) {
  260.         height=Math.abs(height*coefficient);
  261.         Point center = center();
  262.         double dx1 = p1.x - center.x;
  263.         double dy1 = p1.y - center.y;
  264.         double dx2 = p2.x - center.x;
  265.         double dy2 = p2.y - center.y;
  266.         p1 = new Point(center.x + dx1 * coefficient, center.y + dy1 * coefficient);
  267.         p2 = new Point(center.x + dx2 * coefficient, center.y + dy2 * coefficient);
  268.     }
  269. }
  270. class Square extends Rectangle{
  271.     Point p1,p2;
  272.     public Square(Point p1, Point p2) {
  273.         super(p1, p2, Point.distance(p1,p2));
  274.     }
  275.     public double side(){
  276.         return firstSide();
  277.     }
  278.     public Circle circumscribedCircle(){
  279.         return new Circle(center(),diagonal()/2);
  280.     }
  281.     public Circle inscribedCircle(){
  282.         return new Circle(center(),side()/2);
  283.     }
  284. }
  285. class Triangle extends Shape{
  286.     Point p1,p2,p3;
  287.     public Triangle(Point p1, Point p2, Point p3){
  288.         this.p1 = p1;
  289.         this.p2 = p2;
  290.         this.p3 = p3;
  291.     }
  292.  
  293.     public List<Point> vertices(){
  294.         List<Point> l = new ArrayList<>();
  295.         l.add(p1);
  296.         l.add(p2);
  297.         l.add(p3);
  298.         return l;
  299.     }
  300.  
  301.     public Circle circumscribedCircle(){
  302.         double r = (Point.distance(p1,p2)*Point.distance(p3,p2)*Point.distance(p1,p3))/(4*area());
  303.         double xC = (-0.5)*(p1.y*(p2.x*p2.x+p2.y*p2.y-p3.x*p3.x-p3.y*p3.y)+p2.y*(p3.x*p3.x+p3.y*p3.y-p1.x*p1.x-p1.y*p1.y)+p3.y*(p1.x*p1.x+p1.y*p1.y-p2.x*p2.x-p2.y*p2.y))/(p1.x*(p2.y-p3.y)+p2.x*(p3.y-p1.y)+p3.x*(p1.y-p2.y));
  304.         double yC = (0.5)*(p1.x*(p2.x*p2.x+p2.y*p2.y-p3.x*p3.x-p3.y*p3.y)+p2.x*(p3.x*p3.x+p3.y*p3.y-p1.x*p1.x-p1.y*p1.y)+p3.x*(p1.x*p1.x+p1.y*p1.y-p2.x*p2.x-p2.y*p2.y))/(p1.x*(p2.y-p3.y)+p2.x*(p3.y-p1.y)+p3.x*(p1.y-p2.y));
  305.         return new Circle(new Point(xC,yC),r);
  306.     }
  307.     public Circle inscribedCircle(){
  308.         double r = 2*area()/perimeter();
  309.         double xC = ((p1.x + p2.x - 2*p3.x)*((p3.y - p1.y)*(p2.x + p3.x - 2*p1.x) + p1.x*(p2.y + p3.y - 2*p1.y)) - p3.x*(p2.x + p3.x - 2*p1.x)*(p1.y + p2.y - 2*p3.y))/((p2.y + p3.y - 2*p1.y)*(p1.x + p2.x - 2*p3.x) - (p2.x + p3.x - 2*p1.x)*(p1.y + p2.y - 2*p3.y));
  310.         double yC = ((xC - p1.x)*(p2.y + p3.y - 2*p1.y)/(p2.x + p3.x - 2*p1.x)) + p1.y;
  311.         return new Circle(new Point(xC,yC),r);
  312.     }
  313.     public Point orthocenter(){
  314.         double Ax = p3.x - p2.x;
  315.         double Bx = p3.y - p2.y;
  316.         double Cx = p1.x * (p3.x - p2.x) + p1.y * (p3.y - p2.y);
  317.         double Ay = p3.x - p1.x;
  318.         double By = p3.y - p1.y;
  319.         double Cy = p2.x * (p3.x - p1.x) + p2.y * (p3.y - p1.y);
  320.         double det = Ax * By - Ay * Bx;
  321.         double detX = Cx * By - Cy * Bx;
  322.         double detY = Ax * Cy - Ay * Cx;
  323.         return new Point(detX/det,detY/det);
  324.     }
  325.     public Circle ninePointsCircle(){
  326.         Circle c = circumscribedCircle();
  327.         Point p = orthocenter().midpoint(c.center());
  328.         double r = c.radius()/2;
  329.         return new Circle(p,r);
  330.     }
  331.  
  332.     @Override
  333.     public Point center() {
  334.         return new Point((p1.x+p2.x+p3.x)/3,(p1.y+p2.y+p3.y)/3);
  335.     }
  336.  
  337.     @Override
  338.     public double perimeter() {
  339.         return Point.distance(p1,p2)+Point.distance(p1,p3)+Point.distance(p2,p3);
  340.     }
  341.  
  342.     @Override
  343.     public double area() {
  344.         return 0.5*(p1.x*(p2.y-p3.y)+p2.x*(p3.y-p1.y)+p3.x*(p1.y-p2.y));
  345.     }
  346.  
  347.     @Override
  348.     public void translate(Point newCenter) {
  349.         Point currentCenter = center();
  350.         double dX = newCenter.x-currentCenter.x;
  351.         double dY = newCenter.y-currentCenter.y;
  352.         p1.add(dX,dY);
  353.         p2.add(dX,dY);
  354.         p3.add(dX,dY);
  355.     }
  356.  
  357.     @Override
  358.     public void rotate(double angle) {
  359.         Point center = center();
  360.         p1 = p1.rotate(center,angle);
  361.         p2 = p2.rotate(center,angle);
  362.         p3 = p3.rotate(center,angle);
  363.     }
  364.  
  365.     @Override
  366.     public void scale(double coefficient) {
  367.         Point center = center();
  368.         double dX = p1.x-center.x;
  369.         double dY = p1.y-center.y;
  370.         p1 = p1.add(dX*coefficient,dY*coefficient);
  371.         dX = p2.x-center.x;
  372.         dY = p2.y-center.y;
  373.         p2 = p2.add(dX*coefficient,dY*coefficient);
  374.         dX = p3.x-center.x;
  375.         dY = p3.y-center.y;
  376.         p3 = p3.add(dX*coefficient,dY*coefficient);
  377.     }
  378. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement