-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainForm.cs
126 lines (89 loc) · 2.44 KB
/
MainForm.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
namespace PolygonIntersection {
public partial class MainForm : Form {
List<Polygon> polygons = new List<Polygon>();
Polygon player;
public MainForm() {
InitializeComponent();
Paint += new PaintEventHandler(Form1_Paint);
KeyDown += new KeyEventHandler(Form1_KeyDown);
KeyPreview = true;
DoubleBuffered = true;
Polygon p = new Polygon();
p.Points.Add(new Vector(100, 0));
p.Points.Add(new Vector(150, 50));
p.Points.Add(new Vector(100, 150));
p.Points.Add(new Vector(0, 100));
polygons.Add(p);
p = new Polygon();
p.Points.Add(new Vector(50, 50));
p.Points.Add(new Vector(100, 0));
p.Points.Add(new Vector(150, 150));
p.Offset(80, 80);
polygons.Add(p);
p = new Polygon();
p.Points.Add(new Vector(0, 50));
p.Points.Add(new Vector(50,0));
p.Points.Add(new Vector(150,80));
p.Points.Add(new Vector(160,200));
p.Points.Add(new Vector(-10, 190));
p.Offset(300, 300);
polygons.Add(p);
foreach (Polygon polygon in polygons) polygon.BuildEdges();
player = polygons[0];
}
void Form1_Paint(object sender, PaintEventArgs e) {
Vector p1;
Vector p2;
foreach (Polygon polygon in polygons) {
for (int i = 0; i < polygon.Points.Count; i++) {
p1 = polygon.Points[i];
if (i + 1 >= polygon.Points.Count) {
p2 = polygon.Points[0];
} else {
p2 = polygon.Points[i + 1];
}
e.Graphics.DrawLine(new Pen(Color.Black), p1, p2);
}
}
Invalidate();
}
void Form1_KeyDown(object sender, KeyEventArgs e) {
int i = 14;
Vector velocity = new Vector();
switch (e.KeyValue) {
case 32: //SPACE
break;
case 38: // UP
velocity = new Vector(0, -i);
break;
case 40: // DOWN
velocity = new Vector(0, i);
break;
case 39: // RIGHT
velocity = new Vector(i, 0);
break;
case 37: // LEFT
velocity = new Vector(-i, 0);
break;
}
Vector playerTranslation = velocity;
foreach (Polygon polygon in polygons) {
if (polygon == player) continue;
PolygonCollisionResult r = PolygonCollision(player, polygon, velocity);
if (r.WillIntersect) {
playerTranslation = velocity + r.MinimumTranslationVector;
break;
}
}
player.Offset(playerTranslation);
}
}
}