Advertisement
SquirrelInBox

task1

Apr 12th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.63 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.util.Vector;
  4.  
  5. /**
  6.  * Created by Helen on 12.04.2016.
  7.  */
  8. public class Main extends JPanel {
  9.     // x = (a+t)^2+b
  10.     // y = ct+d
  11.     static int width = 800;
  12.     static int height = 600;
  13.  
  14.     public int A = 1;
  15.     public int B = 1;
  16.     public int C = 10;
  17.     public int D = 1;
  18.  
  19.     class DoublePoint{
  20.         public double x;
  21.         public double y;
  22.  
  23.         DoublePoint(double x, double y){
  24.             this.x = x;
  25.             this.y = y;
  26.         }
  27.  
  28.         public double getDistToIntPoint(IntPoint point)
  29.         {
  30.             return Math.sqrt((point.x - x)*(point.x - x) + (point.y - y)*(point.y - y));
  31.         }
  32.     }
  33.  
  34.     class IntPoint {
  35.         public int x;
  36.         public int y;
  37.  
  38.         IntPoint(int x, int y){
  39.             this.x = x;
  40.             this.y = y;
  41.         }
  42.  
  43.         public double getDistToLine(Line ab)
  44.         {
  45.             DoublePoint a = ab.a;
  46.             DoublePoint b = ab.b;
  47.             double A = b.y - a.y;
  48.             double B = -b.x + a.x;
  49.             double C = a.y*(b.x - a.x) - a.x*(b.y - a.y);
  50.             return Math.abs(A*x + B*y + C) / Math.sqrt(A*A + B*B);
  51.         }
  52.     }
  53.  
  54.     class Line{
  55.         DoublePoint a;
  56.         DoublePoint b;
  57.         public Line(DoublePoint a, DoublePoint b)
  58.         {
  59.             this.a = a;
  60.             this.b = b;
  61.         }
  62.     }
  63.  
  64.     private class Parabola{
  65.  
  66.         DoublePoint focus;
  67.         IntPoint point0;
  68.         double p;
  69.  
  70.         Line direct;
  71.  
  72.         public Parabola()
  73.         {
  74.             if (C == 0)
  75.             {
  76.                 return;
  77.             }
  78.  
  79.             p = C / 2.0;
  80.             point0 = new IntPoint(0, 0);
  81.             focus = new DoublePoint(p/2.0, 0);
  82.             direct = new Line(new DoublePoint(-p/2.0, 10),
  83.                     new DoublePoint(-p/2, -10));
  84.         }
  85.     }
  86.  
  87.     public double getDelta(Parabola parabola, IntPoint point)
  88.     {
  89.         double distToFocus = parabola.focus.getDistToIntPoint(point);
  90.         double distToDirect = point.getDistToLine(parabola.direct);
  91.         return distToDirect - distToFocus;
  92.     }
  93.  
  94.     public IntPoint dVSc(Parabola parabola, IntPoint curPoint, double deltaC) {
  95.         IntPoint pD = new IntPoint(curPoint.x, curPoint.y + 1);
  96.         double deltaD = getDelta(parabola, pD);
  97.         if (Math.abs(deltaD) < Math.abs(deltaC))
  98.             return pD;
  99.         return new IntPoint(curPoint.x + 1, curPoint.y + 1);
  100.     }
  101.  
  102.     public IntPoint bVSc(Parabola parabola, IntPoint curPoint, double deltaC) {
  103.         IntPoint pB = new IntPoint(curPoint.x + 1, curPoint.y);
  104.         double deltaB = getDelta(parabola, pB);
  105.         if (Math.abs(deltaB) < Math.abs(deltaC))
  106.             return pB;
  107.         return new IntPoint(curPoint.x + 1, curPoint.y + 1);
  108.     }
  109.  
  110.     public Vector<IntPoint> calculate(Graphics g)
  111.     {
  112.         Vector<IntPoint> resPoints = new Vector<>();
  113.         Parabola parabola = new Parabola();
  114.         IntPoint sP = parabola.point0;
  115.         resPoints.add(sP);
  116.  
  117.         while(sP.x < width / 2) {
  118.             IntPoint pointC = new IntPoint(sP.x + 1, sP.y + 1);
  119.             double deltaC = getDelta(parabola, pointC);
  120.             IntPoint pB = new IntPoint(sP.x + 1, sP.y);
  121.             double deltaB = getDelta(parabola, pB);
  122.             IntPoint pD = new IntPoint(sP.x, sP.y + 1);
  123.             double deltaD = getDelta(parabola, pD);
  124.             if (deltaC > 0) {
  125.                 sP = dVSc(parabola, sP, deltaC);
  126.             } else {
  127.                 sP = bVSc(parabola, sP, deltaC);
  128.             }
  129.             resPoints.add(sP);
  130.         }
  131.         return resPoints;
  132.     }
  133.  
  134.     public void drawPoints(Graphics g, Vector<IntPoint> points)
  135.     {
  136.         int shiftX = width / 2 - A + B;
  137.         int shiftY = height / 2 + D;
  138.         for(IntPoint point : points) {
  139.             g.drawLine(point.x + shiftX, point.y + shiftY,
  140.                     point.x + shiftX, point.y + shiftY);
  141.  
  142.             g.drawLine(point.x + shiftX, -point.y + shiftY,
  143.                     point.x + shiftX, -point.y + shiftY);
  144.         }
  145.     }
  146.  
  147.     @Override
  148.     public void paint(Graphics g)
  149.     {
  150.         super.paint(g);
  151.  
  152.         Vector<IntPoint> points = calculate(g);
  153.         drawPoints(g, points);
  154.         super.repaint();
  155.     }
  156.  
  157.     public static void main(String[] args) {
  158.         Main canv = new Main();
  159.         canv.setPreferredSize(new Dimension(width, height));
  160.  
  161.         JFrame w=new JFrame("Function");
  162.         w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  163.  
  164.         w.getContentPane().add(canv);
  165.         w.pack();
  166.  
  167.         w.setLocationRelativeTo(null);
  168.         w.setVisible(true);
  169.     }
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement