1 #include "pch.h"
2 #include "TLine.h"
3 
4 #include "TTableLayer.h"
5 
6 
TLine(TCollisionComponent * collCmp,char * activeFlag,unsigned int collisionGroup,float x0,float y0,float x1,float y1)7 TLine::TLine(TCollisionComponent* collCmp, char* activeFlag, unsigned int collisionGroup, float x0, float y0, float x1,
8              float y1): TEdgeSegment(collCmp, activeFlag, collisionGroup)
9 {
10 	X0 = x0;
11 	Y0 = y0;
12 	X1 = x1;
13 	Y1 = y1;
14 	maths::line_init(&Line, x0, y0, x1, y1);
15 }
16 
TLine(TCollisionComponent * collCmp,char * activeFlag,unsigned int collisionGroup,struct vector_type * start,struct vector_type * end)17 TLine::TLine(TCollisionComponent* collCmp, char* activeFlag, unsigned int collisionGroup, struct vector_type* start,
18              struct vector_type* end) : TEdgeSegment(collCmp, activeFlag, collisionGroup)
19 {
20 	X0 = start->X;
21 	Y0 = start->Y;
22 	X1 = end->X;
23 	Y1 = end->Y;
24 	maths::line_init(&Line, X0, Y0, X1, Y1);
25 }
26 
Offset(float offset)27 void TLine::Offset(float offset)
28 {
29 	float offX = offset * Line.PerpendicularL.X;
30 	float offY = offset * Line.PerpendicularL.Y;
31 
32 	X0 += offX;
33 	Y0 += offY;
34 	X1 += offX;
35 	Y1 += offY;
36 	maths::line_init(&Line, X0, Y0, X1, Y1);
37 }
38 
FindCollisionDistance(ray_type * ray)39 float TLine::FindCollisionDistance(ray_type* ray)
40 {
41 	return maths::ray_intersect_line(ray, &Line);
42 }
43 
EdgeCollision(TBall * ball,float coef)44 void TLine::EdgeCollision(TBall* ball, float coef)
45 {
46 	CollisionComponent->Collision(
47 		ball,
48 		&Line.RayIntersect,
49 		&Line.PerpendicularL,
50 		coef,
51 		this);
52 }
53 
place_in_grid()54 void TLine::place_in_grid()
55 {
56 	auto edgeMan = TTableLayer::edge_manager;
57 	auto xBox0 = edgeMan->box_x(X0);
58 	auto yBox0 = edgeMan->box_y(Y0);
59 	auto xBox1 = edgeMan->box_x(X1);
60 	auto yBox1 = edgeMan->box_y(Y1);
61 
62 	int dirX = X0 >= X1 ? -1 : 1;
63 	int dirY = Y0 >= Y1 ? -1 : 1;
64 
65 	if (yBox0 == yBox1)
66 	{
67 		if (dirX == 1)
68 		{
69 			while (xBox0 <= xBox1)
70 				edgeMan->add_edge_to_box(xBox0++, yBox0, this);
71 		}
72 		else
73 		{
74 			while (xBox0 >= xBox1)
75 				edgeMan->add_edge_to_box(xBox0--, yBox0, this);
76 		}
77 	}
78 	else if (xBox0 == xBox1)
79 	{
80 		if (dirY == 1)
81 		{
82 			if (yBox0 <= yBox1)
83 			{
84 				do
85 					edgeMan->add_edge_to_box(xBox0, yBox0++, this);
86 				while (yBox0 <= yBox1);
87 			}
88 		}
89 		else if (yBox0 >= yBox1)
90 		{
91 			do
92 				edgeMan->add_edge_to_box(xBox0, yBox0--, this);
93 			while (yBox0 >= yBox1);
94 		}
95 	}
96 	else
97 	{
98 		float yCoord, xCoord;
99 		int indexX1 = xBox0, indexY1 = yBox0;
100 		int bresIndexX = xBox0 + 1, bresIndexY = yBox0 + 1;
101 		auto bresDyDx = (Y0 - Y1) / (X0 - X1);
102 		auto bresXAdd = Y0 - bresDyDx * X0;
103 		edgeMan->add_edge_to_box(xBox0, yBox0, this);
104 		if (dirX == 1)
105 		{
106 			if (dirY == 1)
107 			{
108 				do
109 				{
110 					yCoord = bresIndexY * edgeMan->AdvanceY + edgeMan->Y;
111 					xCoord = (bresIndexX * edgeMan->AdvanceX + edgeMan->X) * bresDyDx + bresXAdd;
112 					if (xCoord >= yCoord)
113 					{
114 						if (xCoord == yCoord)
115 						{
116 							++indexX1;
117 							++bresIndexX;
118 						}
119 						++indexY1;
120 						++bresIndexY;
121 					}
122 					else
123 					{
124 						++indexX1;
125 						++bresIndexX;
126 					}
127 					edgeMan->add_edge_to_box(indexX1, indexY1, this);
128 				}
129 				while (indexX1 != xBox1 || indexY1 != yBox1);
130 			}
131 			else
132 			{
133 				do
134 				{
135 					yCoord = indexY1 * edgeMan->AdvanceY + edgeMan->Y;
136 					xCoord = (bresIndexX * edgeMan->AdvanceX + edgeMan->X) * bresDyDx + bresXAdd;
137 					if (xCoord <= yCoord)
138 					{
139 						if (xCoord == yCoord)
140 						{
141 							++indexX1;
142 							++bresIndexX;
143 						}
144 						--indexY1;
145 					}
146 					else
147 					{
148 						++indexX1;
149 						++bresIndexX;
150 					}
151 					edgeMan->add_edge_to_box(indexX1, indexY1, this);
152 				}
153 				while (indexX1 != xBox1 || indexY1 != yBox1);
154 			}
155 		}
156 		else
157 		{
158 			if (dirY == 1)
159 			{
160 				do
161 				{
162 					xCoord = bresIndexY * edgeMan->AdvanceY + edgeMan->Y;
163 					yCoord = (indexX1 * edgeMan->AdvanceX + edgeMan->X) * bresDyDx + bresXAdd;
164 					if (yCoord >= xCoord)
165 					{
166 						if (yCoord == xCoord)
167 							--indexX1;
168 						++indexY1;
169 						++bresIndexY;
170 					}
171 					else
172 					{
173 						--indexX1;
174 					}
175 					edgeMan->add_edge_to_box(indexX1, indexY1, this);
176 				}
177 				while (indexX1 != xBox1 || indexY1 != yBox1);
178 			}
179 			else
180 			{
181 				do
182 				{
183 					yCoord = indexY1 * edgeMan->AdvanceY + edgeMan->Y;
184 					xCoord = (indexX1 * edgeMan->AdvanceX + edgeMan->X) * bresDyDx + bresXAdd;
185 					if (xCoord <= yCoord)
186 					{
187 						if (xCoord == yCoord)
188 							--indexX1;
189 						--indexY1;
190 					}
191 					else
192 					{
193 						--indexX1;
194 					}
195 					edgeMan->add_edge_to_box(indexX1, indexY1, this);
196 				}
197 				while (indexX1 != xBox1 || indexY1 != yBox1);
198 			}
199 		}
200 	}
201 }
202