-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShape.cs
142 lines (124 loc) · 4.88 KB
/
Shape.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
namespace ConsoleGraphic
{
public class Shape
{
protected Vector2 position;
protected char[] texture;
public Shape(Vector2 position, char[] texture)
{
this.position = position;
this.texture = texture;
}
public virtual char[] Draw(Graphics graphics)
{
return new char[graphics.width * graphics.height];
}
public void Move(Vector2 direction)
{
position += direction;
}
public void SetPosition(Vector2 newPosition)
{
position = newPosition;
}
public Vector2 GetPosition()
{
return position;
}
}
public class Circle : Shape
{
protected float size;
/// <summary>
/// Круг с градиентом
/// </summary>
/// <param name="position"></param>
/// <param name="size"></param>
/// <param name="texture">принмает массив из 4х символов</param>
public Circle(Vector2 position, float size, char[] texture) : base(position, texture)
{
this.size = size;
}
override public char[] Draw(Graphics graphics)
{
char[] buffer = new char[graphics.buffer.Length];
for (int i = 0; i < graphics.width; i++)
for (int j = 0; j < graphics.height; j++)
{
float x = (float)i / graphics.width * 2.0f * graphics.multiplier - 1.0f * graphics.multiplier;
float y = (float)j / graphics.height * 2.0f * graphics.multiplier - 1.0f * graphics.multiplier;
x *= graphics.aspect * graphics.pixelAspect;
float f = (x - position.x) * (x - position.x) + (y - position.y) * (y - position.y);
char pixel;
if (f < size)
{
if (f < size / 8)
pixel = texture[3];
else if (f < size / 3)
pixel = texture[2];
else if (f < size / 1.5f)
pixel = texture[1];
else
pixel = texture[0];
buffer[i + j * graphics.width] = pixel;
}
}
return buffer;
}
}
public class SolidRectangle : Shape
{
protected Vector2 size;
/// <summary>
/// одноцветный прямоугольник
/// </summary>
/// <param name="position"></param>
/// <param name="size"></param>
/// <param name="texture">принмает массив из 1го символа</param>
public SolidRectangle(Vector2 position, Vector2 size, char[] texture) : base(position, texture)
{
this.size = size;
}
override public char[] Draw(Graphics graphics)
{
char[] buffer = graphics.buffer;
for (int i = 0; i < graphics.width; i++)
for (int j = 0; j < graphics.height; j++)
{
float x = (float)i / graphics.width * 2.0f * graphics.multiplier - 1.0f * graphics.multiplier;
float y = (float)j / graphics.height * 2.0f * graphics.multiplier - 1.0f * graphics.multiplier;
x *= graphics.aspect * graphics.pixelAspect;
if (x > position.x && x < position.x + size.x && y > position.y && y < position.y + size.y)
buffer[i + j * graphics.width] = texture[0];
}
return buffer;
}
}
public class Line: Shape
{
protected Vector2 endPosition;
protected float k;
protected float a;
public Line(Vector2 position, Vector2 endPosition, char[] texture) : base(position, texture)
{
this.endPosition = endPosition;
k = (endPosition.y - position.y) / (endPosition.x - position.x);
a = position.y - position.x * k;
}
override public char[] Draw(Graphics graphics)
{
char[] buffer = graphics.buffer;
for (int i = 0; i < graphics.width; i++)
for (int j = 0; j < graphics.height; j++)
{
float x = (float)i / graphics.width * 2.0f * graphics.multiplier - 1.0f * graphics.multiplier;
float y = (float)j / graphics.height * 2.0f * graphics.multiplier - 1.0f * graphics.multiplier;
x *= graphics.aspect * graphics.pixelAspect;
float f = k * x + a;
if (y > f - 0.05f && y < f + 0.05f && x > position.x && x < endPosition.x)
buffer[i + j * graphics.width] = texture[0];
}
return buffer;
}
}
}