Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.*;
- import java.util.Vector;
- /**
- * Created by Helen on 12.04.2016.
- */
- public class Main extends JPanel {
- // x = (a+t)^2+b
- // y = ct+d
- static int width = 800;
- static int height = 600;
- public int A = 1;
- public int B = 1;
- public int C = 10;
- public int D = 1;
- class DoublePoint{
- public double x;
- public double y;
- DoublePoint(double x, double y){
- this.x = x;
- this.y = y;
- }
- public double getDistToIntPoint(IntPoint point)
- {
- return Math.sqrt((point.x - x)*(point.x - x) + (point.y - y)*(point.y - y));
- }
- }
- class IntPoint {
- public int x;
- public int y;
- IntPoint(int x, int y){
- this.x = x;
- this.y = y;
- }
- public double getDistToLine(Line ab)
- {
- DoublePoint a = ab.a;
- DoublePoint b = ab.b;
- double A = b.y - a.y;
- double B = -b.x + a.x;
- double C = a.y*(b.x - a.x) - a.x*(b.y - a.y);
- return Math.abs(A*x + B*y + C) / Math.sqrt(A*A + B*B);
- }
- }
- class Line{
- DoublePoint a;
- DoublePoint b;
- public Line(DoublePoint a, DoublePoint b)
- {
- this.a = a;
- this.b = b;
- }
- }
- private class Parabola{
- DoublePoint focus;
- IntPoint point0;
- double p;
- Line direct;
- public Parabola()
- {
- if (C == 0)
- {
- return;
- }
- p = C / 2.0;
- point0 = new IntPoint(0, 0);
- focus = new DoublePoint(p/2.0, 0);
- direct = new Line(new DoublePoint(-p/2.0, 10),
- new DoublePoint(-p/2, -10));
- }
- }
- public double getDelta(Parabola parabola, IntPoint point)
- {
- double distToFocus = parabola.focus.getDistToIntPoint(point);
- double distToDirect = point.getDistToLine(parabola.direct);
- return distToDirect - distToFocus;
- }
- public IntPoint dVSc(Parabola parabola, IntPoint curPoint, double deltaC) {
- IntPoint pD = new IntPoint(curPoint.x, curPoint.y + 1);
- double deltaD = getDelta(parabola, pD);
- if (Math.abs(deltaD) < Math.abs(deltaC))
- return pD;
- return new IntPoint(curPoint.x + 1, curPoint.y + 1);
- }
- public IntPoint bVSc(Parabola parabola, IntPoint curPoint, double deltaC) {
- IntPoint pB = new IntPoint(curPoint.x + 1, curPoint.y);
- double deltaB = getDelta(parabola, pB);
- if (Math.abs(deltaB) < Math.abs(deltaC))
- return pB;
- return new IntPoint(curPoint.x + 1, curPoint.y + 1);
- }
- public Vector<IntPoint> calculate(Graphics g)
- {
- Vector<IntPoint> resPoints = new Vector<>();
- Parabola parabola = new Parabola();
- IntPoint sP = parabola.point0;
- resPoints.add(sP);
- while(sP.x < width / 2) {
- IntPoint pointC = new IntPoint(sP.x + 1, sP.y + 1);
- double deltaC = getDelta(parabola, pointC);
- IntPoint pB = new IntPoint(sP.x + 1, sP.y);
- double deltaB = getDelta(parabola, pB);
- IntPoint pD = new IntPoint(sP.x, sP.y + 1);
- double deltaD = getDelta(parabola, pD);
- if (deltaC > 0) {
- sP = dVSc(parabola, sP, deltaC);
- } else {
- sP = bVSc(parabola, sP, deltaC);
- }
- resPoints.add(sP);
- }
- return resPoints;
- }
- public void drawPoints(Graphics g, Vector<IntPoint> points)
- {
- int shiftX = width / 2 - A + B;
- int shiftY = height / 2 + D;
- for(IntPoint point : points) {
- g.drawLine(point.x + shiftX, point.y + shiftY,
- point.x + shiftX, point.y + shiftY);
- g.drawLine(point.x + shiftX, -point.y + shiftY,
- point.x + shiftX, -point.y + shiftY);
- }
- }
- @Override
- public void paint(Graphics g)
- {
- super.paint(g);
- Vector<IntPoint> points = calculate(g);
- drawPoints(g, points);
- super.repaint();
- }
- public static void main(String[] args) {
- Main canv = new Main();
- canv.setPreferredSize(new Dimension(width, height));
- JFrame w=new JFrame("Function");
- w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- w.getContentPane().add(canv);
- w.pack();
- w.setLocationRelativeTo(null);
- w.setVisible(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement