import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;

public class QuadSubdivision extends JPanel  {
    Point2D.Double[] points;
    int n;

    public QuadSubdivision(Point2D.Double[] p, int m) {
	points = p;  n = m;
        setBackground(Color.white);
    }    
    
    public void paintComponent(Graphics gfx) {
        super.paintComponent(gfx);
        Graphics2D g = (Graphics2D) gfx;
	g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
			   RenderingHints.VALUE_ANTIALIAS_ON);

	g.setPaint(new Color(0.5f, 0.5f, 1f));
	g.setStroke(new BasicStroke(2f));
	g.draw(new QuadCurve2D.Double(points[0].x, points[0].y, 
				      points[1].x, points[1].y, 
				      points[2].x, points[2].y));
	g.setPaint(new Color(1f, 0.5f, 0.5f));
	g.setStroke(new BasicStroke(1.5f));
	drawCurve(g, points[0], points[1], points[2], n);
	
    }

    public void drawCurve(Graphics2D g, Point2D.Double p0,
			  Point2D.Double p1, Point2D.Double p2, int n) {
	if (n == 0) {
	    g.draw(new Line2D.Double(p0, p2));
	    return;
	}
	
	Point2D.Double q1 = new Point2D.Double((p0.x+p1.x)/2,
					       (p0.y+p1.y)/2);
	Point2D.Double q2 = new Point2D.Double((p1.x+p2.x)/2,
					       (p1.y+p2.y)/2);
	Point2D.Double q3 = new Point2D.Double((q1.x+q2.x)/2,
					       (q1.y+q2.y)/2);
	drawCurve(g, p0, q1, q3, n-1);
	drawCurve(g, q3, q2, p2, n-1);
    }
	
    

    public static void main(String[] args) {
	Point2D.Double[] points = new Point2D.Double[3];
	for (int i = 0; i < 3; i++) 
	    points[i] = new Point2D.Double(Double.parseDouble(args[2*i]),
					   Double.parseDouble(args[2*i+1]));
	int n = Integer.parseInt(args[6]);
        QuadSubdivision inscribed = new QuadSubdivision(points, n);

        JFrame frame = new JFrame("QuadSubdivision");
	frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.getContentPane().add(inscribed, BorderLayout.CENTER);
        inscribed.setPreferredSize(new Dimension(300, 300));
        frame.pack();
        frame.setVisible(true);
    }
}
