1 | using System; |
---|
2 | using System.Collections.Generic; |
---|
3 | using Jypeli; |
---|
4 | |
---|
5 | public class Sq : PhysicsGame |
---|
6 | { |
---|
7 | List<Vector> ps, crc; |
---|
8 | double rad = 50, radc = 0.01, mag = 2; |
---|
9 | |
---|
10 | public override void Begin() |
---|
11 | { |
---|
12 | Window.Width = 1200; Window.Height = 800; |
---|
13 | Mouse.IsCursorVisible = true; |
---|
14 | Level.Background.Color = Color.Black; |
---|
15 | |
---|
16 | ps = CreateSquare(100, 10); |
---|
17 | crc = CreateCircle(50, 150); |
---|
18 | } |
---|
19 | |
---|
20 | // s = length of side, d = distance between pts |
---|
21 | List<Vector> CreateSquare(int s, int d) |
---|
22 | { |
---|
23 | List<Vector> sq = new List<Vector>(); |
---|
24 | for (int i = -s; i <= s; i += d) sq.Add(new Vector(-s, i)); |
---|
25 | for (int i = -s; i <= s; i += d) sq.Add(new Vector(i, s)); |
---|
26 | for (int i = s; i >= -s; i -= d) sq.Add(new Vector(s, i)); |
---|
27 | for (int i = s; i >= -s; i -= d) sq.Add(new Vector(i, -s)); |
---|
28 | return sq; |
---|
29 | } |
---|
30 | |
---|
31 | // rad = starting radius, n = number of pts |
---|
32 | List<Vector> CreateCircle(int rad, int n) |
---|
33 | { |
---|
34 | List<Vector> crc = new List<Vector>(); |
---|
35 | for (int i = 0; i <= n; i++) |
---|
36 | crc.Add(new Vector(Math.Sin(i) * rad, Math.Cos(i) * rad)); |
---|
37 | return crc; |
---|
38 | } |
---|
39 | |
---|
40 | // rotates any shape clockwise or anti if reversed |
---|
41 | void Rotate(List<Vector> vs, bool reverse = false) |
---|
42 | { |
---|
43 | double d = Time.SinceLastUpdate.TotalSeconds; |
---|
44 | if (reverse) d = -d; |
---|
45 | for (int i = 0; i < vs.Count; i++) |
---|
46 | vs[i] = new Vector(vs[i].X * Math.Cos(-d) - vs[i].Y * Math.Sin(-d), |
---|
47 | vs[i].X * Math.Sin(-d) + vs[i].Y * Math.Cos(-d)); |
---|
48 | } |
---|
49 | |
---|
50 | // does everything |
---|
51 | protected override void Paint(Canvas c) |
---|
52 | { |
---|
53 | c.BrushColor = Color.Red; |
---|
54 | |
---|
55 | // draws the square and squeezes it |
---|
56 | for (int i = 0; i < ps.Count; i++) |
---|
57 | { |
---|
58 | c.DrawLine(ps[i % ps.Count], ps[(i + ps.Count / 4) % ps.Count]); |
---|
59 | //ps[i] = Vector.FromLengthAndAngle(ps[i].Magnitude + mag, ps[i].Angle); |
---|
60 | if (ps[i].Magnitude < 1 || ps[i].Magnitude > 200) mag = -mag; |
---|
61 | ps[i] += new Vector(5, 5); |
---|
62 | } |
---|
63 | |
---|
64 | c.BrushColor = Color.Blue; |
---|
65 | |
---|
66 | // draws the circle and squeezes it |
---|
67 | for (int i = 0; i < crc.Count; i++) |
---|
68 | { |
---|
69 | c.DrawLine(crc[i % crc.Count], crc[(i + 2) % crc.Count]); |
---|
70 | crc[i] = new Vector(Math.Sin(i) * rad, Math.Cos(i) * rad); |
---|
71 | if (rad < 1 || rad > 100) radc = -radc; |
---|
72 | rad -= radc; |
---|
73 | } |
---|
74 | |
---|
75 | Rotate(ps); Rotate(crc, true); |
---|
76 | |
---|
77 | base.Paint(c); |
---|
78 | } |
---|
79 | } |
---|