logo

QuadratQuiz

Solution

  1. Draw a square with width N
    big starting rectangle
  2. Cut of a rectangular stripe at the right
    cutting the stripe
  3. Move that stripe to the center of the resulting rest rectangle
    center the stripe
  4. Draw a circle around the corners of the inner rectangle
    draw the circle
  5. Turn the inner rectangle along the circle, so that the upper right corner is located where the upper left corner has been.
    rotated
  6. If the width of the cut was correct, all corners will be positioned on the resulting outer rectangle.
    small arcs proving correctness
  7. Well. That was a well-done cut off. How would a cut to small look like?
    example to small
  8. ... and to big ...
    example to big
  9. Now let's introduce some names:
    Note: the Image is now vertical flipped, but because of symetry that's without special meaning.
    named points, lines and triangles
  10. Now let's start the real math:
    N=1
    We are only interested in relations, so let's define the length of the whole square as 1.
    That's allowed, since we only deal with lengths.
    Imagine we measure with centimeters vs. inches or dots.
    The numbers will change, but the relations are constant.
    c=?
    Let's not forget, what we're fighting for. c is the searched item.
    a=1/2 - c
    That's allmost the definition of a.
    N = b + y
    Both hights of the triangles are N (=1) in Sum.
    N=(a + c) + y
    Phythagoras for the red triangle.
    We replace N:=1
    1 = (a + c) + y
    1 = (a + c) + y
    Well: a + c = N/2 let's use that knowledge:
    1 = (1/2) + y
    1 = (1/4) + y
    3/4 = y
    y = √(3/4)
    c=a + b
    Phythagoras for the yellow triangle. From above we insert a:=1/2 - c
    c=(1/2 - c) + b
    We isolate b
    c - (1/2 - c) = b
    Well (x-y) = x - 2xy + y, we replace:
    c - ((1/2) - 2*1/2c + c) = b
    c - ((1/2) - c + c) = b
    Since x - (y + z) = x - y - z: c - (1/2) + c - c = b
    c vanisches - that's comfortable! - (1/2) + c = b
    - (1/4) + c = b
    c = b + (1/4)
    Combine the last two results and the observation b + y = 1 from above:
    b = 1 - y
    y = √(3/4)
    c = b + 1/4
    c = (1 - y) + 1/4
    c = 1 - 2y + y + 1/4
    c = 1 - 2y + y + 1/4
    c = 1 + 1/4 - 2y + y
    c = 1 + 1/4 - 2(√(3/4) + 3/4
    c = 1 + 1/4 + 3/4 - 2(√(3/4)
    c = 2 - 2√(3/4)
    c = 2 - 2√(3)/√(4)
    c = 2 - 2√(3)/2
    c = 2 - √(3)

    c = 2 - √(3)
    Solution
  11. c ≈ 0.26795 if you prefer to have it decimal.
  12. To generate a quick drawing with nearly fitting Integer-Values use N=15 and c=4
  13. The Program to create the drawings which are the base for the drawings here uses N=480 and c=128 (which should be about 128.62).

Here is the program-listing which helped me, solving the diversion.

import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

interface Drawable
{
	public void draw (Graphics g);
}

class Quadrat implements Drawable
{
	private int x, y, w, h;
	private int cut = 0;
	private java.util.List elements;
	
	public Quadrat (int x, int y, int w, int h)
	{
		this.x = x;
		this.y = y;
		this.w = w;
		this.h = h;
		elements = new ArrayList ();
		elements.add (this);
	}
	
	public void clear ()
	{
		elements.clear ();
		elements.add (this);
	}
	
	public void draw (Graphics g)
	{
		g.setColor (Color.BLACK);
		g.drawRect (x, y, w, h);
		g.setColor (Color.WHITE);
		g.fillRect (x+1, y+1, w-2, h-2);
	}

	public void paint (Graphics g)
	{
		for (Drawable elem : elements)
		{
			elem.draw (g);
		}
	}
	
	public void cut (int c)
	{
		cut = c;
		elements.add (new Drawable () 
		{
			final int ix = x;
			final int iy = y;
			final int xi = w - cut;
			final int ha = h;
			
			public void draw (Graphics g)
			{
				g.setColor (Color.RED);
				g.drawRect (ix, iy, xi, ha);
			}
		});
	}
	
	class P
	{
		public int x, y;
		public P ()	{ x = 0; y = 0;}
		public P (int c, int r)	{ x = c; y = r;}
		
		int distance (P p2)
		{
			int dx = p2.x - x;
			int dy = p2.y - y;
			return (int) Math.sqrt (dx * dx + dy * dy);
		}
	}
	
	public void center ()
	{
		final P center = new P ();
		center.y = y + (h/2);
		center.x = x + ((w-cut)/2);
		final P diagLio = new P (x, y);
		final P diagReu = new P (x + (w-cut), y + h);
		/*
		*/
		final P mitte = new P (center.x - (cut/2), y);
		
		elements.add (new Drawable () 
		{
			public void draw (Graphics g)
			{
				/*
				diag:
				*/
				g.setColor (Color.BLACK);
				g.drawLine (diagLio.x, diagLio.y, diagReu.x, diagReu.y);
				g.drawLine (diagLio.x, diagReu.y, diagReu.x, diagLio.y);

				g.setColor (new Color (255, 0, 0, 128));
				g.fillRect (mitte.x, mitte.y, cut, h);
			}
		});
		
	}
	
	public void circle ()
	{
		final P mitte = new P (x + ((w-cut)/2),  y + (h/2));
		// final P mitte = new P (center.x - (cut/2), y + h/2);
		final P lio = new P (mitte.x - (cut/2), y);
		final int radius = mitte.distance (lio);
		
		elements.add (new Drawable () 
		{
			public void draw (Graphics g)
			{
				g.setColor (new Color (0, 255, 0, 128));
				g.fillOval (mitte.x-radius, mitte.y-radius, 2*radius, 2*radius);
			}
		});
		
	}	
		
	public void kreise ()
	{
		final P mitte = new P (x + ((w-cut)/2),  y + (h/2));
		final P lio = new P (mitte.x - (cut/2), y);
		final int radius = mitte.distance (lio);
		
		int dx = (x + w - cut) - mitte.x;
		int dy = (int) Math.sqrt (radius * radius + dx * dx);
		
		elements.add (new Drawable () 
		{
			public void draw (Graphics g)
			{
				g.setColor (Color.BLACK);
				g.drawOval (mitte.x - (cut/2), y-(cut), 2*cut, 2*cut);
				g.drawOval (mitte.x - (cut/2) - (cut), y + h - cut, 2*cut, 2*cut);
			}
		});
	}

		
	public void poly ()
	{
		final P mitte = new P (x + ((w-cut)/2),  y + (h/2));
		final P lio = new P (mitte.x - (cut/2), y);
		final int radius = mitte.distance (lio);
		
		int dx = (x + w - cut) - mitte.x;
		int dy = (int) Math.sqrt (radius * radius + dx * dx);

		Polygon p = new Polygon ();
		p.addPoint (mitte.x + (cut/2), y);
		p.addPoint (mitte.x + dx, (mitte.y + dy) - h);
		p.addPoint (lio.x, y + h);
		// p.addPoint (x, y + h - (dy - mitte.y) - y);
		p.addPoint (x, h - (dy - mitte.y));
	
		final Polygon poly = p;
		
		elements.add (new Drawable () 
		{
			public void draw (Graphics g)
			{
				g.setColor (Color.BLACK);
				g.drawPolygon (poly);
			}
		});
	}
	
	public void values ()
	{
		final P mitte = new P (x + ((w-cut)/2),  y + (h/2));
		// final P mitte = new P (center.x - (cut/2), y + h/2);
		final P lio = new P (mitte.x - (cut/2), y);
		final int radius = mitte.distance (lio);
		
		int dx = (x + w - cut) - mitte.x;
		int dy = (int) Math.sqrt (radius * radius + dx * dx);
		
		System.out.println ("x = " + x);
		System.out.println ("y = " + y);
		System.out.println ("w = " + w);
		System.out.println ("h = " + h);
		System.out.println ("cut= " + cut);
		System.out.println ("mitte.x = " + mitte.x);
		System.out.println ("mitte.y = " + mitte.y);
		System.out.println ("radius = " + radius);
		System.out.println ("radius = " + radius);
		System.out.println ("dx = " + dx);
		System.out.println ("dy = " + dy);
	}
}

class CanvasPanel extends JPanel 
{
	private Quadrat q; //  = new Quadrat (50, 50, 240*2, 240 * 2);
	
	public CanvasPanel (Quadrat q)
	{
		this.q = q;
	}
	
	public void paint (Graphics g)
	{
		super.paint (g);
		q.paint (g);
	}
}
/**
	QuadraturDesKreises

	@author Stefan Wagner
	@date Fr Apr 21 21:55:55 CEST 2006

*/
public class QuadraturDesKreises extends JFrame implements ActionListener
{
	private static final String progname = "QuadraturDesKreises 0.1";

	private JTextField x;
	private CanvasPanel mitte;
	private JButton jb;
	private Quadrat q = new Quadrat (150, 50, 240*2, 240 * 2);
		
	/** */
	public QuadraturDesKreises (String sx)
	{
		super (progname);
		JPanel mainpanel = new JPanel ();
		mainpanel.setLayout (new BorderLayout ());
		this.getContentPane ().add (mainpanel);

		JPanel nord = new JPanel ();
		nord.setLayout (new BoxLayout (nord, BoxLayout.LINE_AXIS));
		nord.add (new JLabel (" cut: "));
		x = new JTextField (sx);
		nord.add (x);

		mitte = new CanvasPanel (q);
		JPanel sued =  fillPanelWithButtons (new String [] {"cut",  "center", "circle", "arcs", "poly", "values", "clear", "paint", "exit"});

		mainpanel.add (nord, BorderLayout.NORTH);
		// mainpanel.add (jtfb, BorderLayout.EAST);
		mainpanel.add (mitte, BorderLayout.CENTER);
		mainpanel.add (sued, BorderLayout.SOUTH);

		setSize (240*3, 240*3);
		setLocation (100, 100);
		setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
		setVisible (true);
		q.cut (Integer.parseInt (sx));
		q.center ();
		q.circle ();
		q.kreise ();
		q.poly ();
	}

	private JPanel fillPanelWithButtons (String [] names)
	{
		JPanel sued = new JPanel ();
		sued.setLayout (new BoxLayout (sued, BoxLayout.LINE_AXIS));
		for (String name : names)
		{
			JButton b = new JButton (name);
			b.addActionListener (this);
			sued.add (b);
		}
		return sued;
	}
	
	private void center ()
	{
		Toolkit tk = Toolkit.getDefaultToolkit ();
		Dimension screen = tk.getScreenSize ();
		Dimension d = getSize ();
		setLocation ((screen.width - d.width) / 2, (screen.height - d.height) / 2);
	}

	public void actionPerformed (ActionEvent e)
	{
		String cmd = e.getActionCommand ();
		if (cmd.equals ("cut"))
		{
			int cut = Integer.parseInt (x.getText ());
			System.out.println ("cut: " + cut);
			q.cut (cut);
			mitte.updateUI ();
		}
		else if (cmd.equals ("center"))
		{
			q.center ();
			mitte.updateUI ();
		}
		else if (cmd.equals ("circle"))
		{
			q.circle ();
			mitte.updateUI ();
		}
		else if (cmd.equals ("arcs"))
		{
			q.kreise ();
			mitte.updateUI ();
		}
		else if (cmd.equals ("poly"))
		{
			q.poly ();
			mitte.updateUI ();
		}
		else if (cmd.equals ("values"))
		{
			q.values ();
		}
		else if (cmd.equals ("paint"))
		{
			int cut = Integer.parseInt (x.getText ());
			System.out.println ("cut: " + cut);
			q.cut (cut);
			q.center ();
			q.circle ();
			q.kreise ();
			q.poly ();
			mitte.updateUI ();
			System.out.println ("paint");
		}
		else if (cmd.equals ("clear"))
		{
			q.clear ();
			mitte.updateUI ();
			System.out.println ("clear");
		}
		else if (cmd.equals ("exit"))
		{
			System.out.println ("exit");
			System.exit (0);
		}
		else
		{
			System.out.println ("unhandled");
		}
	}

	/** */
	public static void main (String args[])
	{
		String param = "128";
		if (args.length == 1)
		{
			param = args[0];
		}

		new QuadraturDesKreises (param);
	}

	/** */
	public static void usage ()
	{
		System.out.println ("Usage:\tjava QuadraturDesKreises [cutwidth]");
	}
}
logo