Advertisement
Guest User

Untitled

a guest
Dec 19th, 2012
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.55 KB | None | 0 0
  1. //Copyright (c) 2002, Art Gittleman
  2. //This example is provided WITHOUT ANY WARRANTY
  3. //either expressed or implied.
  4.  
  5. /* Start with the seven polygons forming
  6. * a square. Drag them with the mouse, and rotate
  7. * them with the F and B keys to form other shapes.
  8. */
  9.  
  10. using System;
  11. using System.Drawing;
  12. using System.Drawing.Drawing2D;
  13. using System.Windows.Forms;
  14. public class TangramSolver : Form {
  15. private PointF[][] vertices = new PointF[7][];
  16. private Brush[] color = {Brushes.Red,Brushes.Blue,Brushes.Yellow,
  17. Brushes.Magenta,Brushes.Cyan,Brushes.Pink,Brushes.Orange};
  18. private GraphicsPath[] polygon = new GraphicsPath[7];
  19. private Region[] region = new Region[7];
  20. private float[] oldX = new float[7];
  21. private float[] oldY = new float[7];
  22. private int index;
  23.  
  24. public TangramSolver() {
  25. Size = new Size(500,500);
  26. Text = "Tangram Solver";
  27. BackColor = Color.White;
  28. vertices[0] = new PointF[3]
  29. {new PointF(0,200), new PointF(100,100),
  30. new PointF(200,200)};
  31. vertices[1] = new PointF[3]
  32. {new PointF(100,100), new PointF(200,0),
  33. new PointF(200,200)};
  34. vertices[2] = new PointF[3]
  35. {new PointF(0,0), new PointF(100,0), new Point(0,100)};
  36. vertices[3] = new PointF[3]
  37. {new PointF(0,100), new PointF(50,150),
  38. new PointF(0,200)};
  39. vertices[4] = new PointF[3]
  40. {new PointF(50,50), new PointF(150,50),
  41. new PointF(100,100)};
  42. vertices[5] = new PointF[4]
  43. {new PointF(0,100), new PointF(50,50),
  44. new PointF(100,100), new PointF(50,150)};
  45. vertices[6] = new PointF[4]
  46. {new PointF(100,0), new PointF(200,0),
  47. new PointF(150,50), new PointF(50,50)};
  48. for (int i = 0; i < vertices.Length; i++) {
  49. polygon[i] = new GraphicsPath();
  50. polygon[i].AddPolygon(vertices[i]);
  51. region[i] = new Region(polygon[i]);
  52. }
  53. }
  54. protected override void OnMouseDown(MouseEventArgs e) {
  55. bool found = false;
  56. int i = 0;
  57. while(!found && i < region.Length){
  58. RectangleF[] scans =
  59. region[i].GetRegionScans(new Matrix());
  60. int j = 0;
  61. while(!found && j < scans.Length){
  62. if (scans[j].Contains(e.X, e.Y)){
  63. oldX[i] = e.X;
  64. oldY[i] = e.Y;
  65. index = i;
  66. found = true;
  67. }
  68. j++;
  69. }
  70. i++;
  71. }
  72. base.OnMouseDown(e);
  73. }
  74. protected override void OnMouseUp(MouseEventArgs e) {
  75. region[index].Translate(e.X-oldX[index], e.Y-oldY[index]);
  76. oldX[index] = e.X;
  77. oldY[index] = e.Y;
  78. Invalidate();
  79. base.OnMouseUp(e);
  80. }
  81. protected override void OnPaint(PaintEventArgs e) {
  82. Graphics g = e.Graphics;
  83. for (int i = 0; i < region.Length; i++)
  84. g.FillRegion(color[i], region[i]);
  85. base.OnPaint(e);
  86. }
  87. protected override void OnKeyPress(KeyPressEventArgs e) {
  88. float angle = 0;
  89. switch(e.KeyChar){
  90. case 'b' : angle = -3;
  91. break;
  92. case 'B' : angle = -30;
  93. break;
  94. case 'f' : angle = 3;
  95. break;
  96. case 'F' : angle = 30;
  97. break;
  98. }
  99. Matrix m = new Matrix();
  100. m.RotateAt(angle, new PointF(oldX[index], oldY[index]));
  101. if (e.KeyChar == 'R') { }
  102. region[index].Transform(m);
  103. Invalidate();
  104. base.OnKeyPress(e);
  105. }
  106. public static void Main() {
  107. Application.Run(new TangramSolver());
  108. }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement