Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Copyright (c) 2002, Art Gittleman
- //This example is provided WITHOUT ANY WARRANTY
- //either expressed or implied.
- /* Start with the seven polygons forming
- * a square. Drag them with the mouse, and rotate
- * them with the F and B keys to form other shapes.
- */
- using System;
- using System.Drawing;
- using System.Drawing.Drawing2D;
- using System.Windows.Forms;
- public class TangramSolver : Form {
- private PointF[][] vertices = new PointF[7][];
- private Brush[] color = {Brushes.Red,Brushes.Blue,Brushes.Yellow,
- Brushes.Magenta,Brushes.Cyan,Brushes.Pink,Brushes.Orange};
- private GraphicsPath[] polygon = new GraphicsPath[7];
- private Region[] region = new Region[7];
- private float[] oldX = new float[7];
- private float[] oldY = new float[7];
- private int index;
- public TangramSolver() {
- Size = new Size(500,500);
- Text = "Tangram Solver";
- BackColor = Color.White;
- vertices[0] = new PointF[3]
- {new PointF(0,200), new PointF(100,100),
- new PointF(200,200)};
- vertices[1] = new PointF[3]
- {new PointF(100,100), new PointF(200,0),
- new PointF(200,200)};
- vertices[2] = new PointF[3]
- {new PointF(0,0), new PointF(100,0), new Point(0,100)};
- vertices[3] = new PointF[3]
- {new PointF(0,100), new PointF(50,150),
- new PointF(0,200)};
- vertices[4] = new PointF[3]
- {new PointF(50,50), new PointF(150,50),
- new PointF(100,100)};
- vertices[5] = new PointF[4]
- {new PointF(0,100), new PointF(50,50),
- new PointF(100,100), new PointF(50,150)};
- vertices[6] = new PointF[4]
- {new PointF(100,0), new PointF(200,0),
- new PointF(150,50), new PointF(50,50)};
- for (int i = 0; i < vertices.Length; i++) {
- polygon[i] = new GraphicsPath();
- polygon[i].AddPolygon(vertices[i]);
- region[i] = new Region(polygon[i]);
- }
- }
- protected override void OnMouseDown(MouseEventArgs e) {
- bool found = false;
- int i = 0;
- while(!found && i < region.Length){
- RectangleF[] scans =
- region[i].GetRegionScans(new Matrix());
- int j = 0;
- while(!found && j < scans.Length){
- if (scans[j].Contains(e.X, e.Y)){
- oldX[i] = e.X;
- oldY[i] = e.Y;
- index = i;
- found = true;
- }
- j++;
- }
- i++;
- }
- base.OnMouseDown(e);
- }
- protected override void OnMouseUp(MouseEventArgs e) {
- region[index].Translate(e.X-oldX[index], e.Y-oldY[index]);
- oldX[index] = e.X;
- oldY[index] = e.Y;
- Invalidate();
- base.OnMouseUp(e);
- }
- protected override void OnPaint(PaintEventArgs e) {
- Graphics g = e.Graphics;
- for (int i = 0; i < region.Length; i++)
- g.FillRegion(color[i], region[i]);
- base.OnPaint(e);
- }
- protected override void OnKeyPress(KeyPressEventArgs e) {
- float angle = 0;
- switch(e.KeyChar){
- case 'b' : angle = -3;
- break;
- case 'B' : angle = -30;
- break;
- case 'f' : angle = 3;
- break;
- case 'F' : angle = 30;
- break;
- }
- Matrix m = new Matrix();
- m.RotateAt(angle, new PointF(oldX[index], oldY[index]));
- if (e.KeyChar == 'R') { }
- region[index].Transform(m);
- Invalidate();
- base.OnKeyPress(e);
- }
- public static void Main() {
- Application.Run(new TangramSolver());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement