1
1
# frozen_string_literal: true
2
2
3
- # Module provides some Triangle generation and manipulation methods to DragonRuby Game Toolkit.
3
+ # Module provides methods for manipulating shapes to DragonRuby Game Toolkit.
4
4
# By D Schaedler. Released under MIT License.
5
5
# https://github.com/DSchaedler/dinraal
6
6
module Dinraal
7
- # Calculates a `border` `rect` for the bounding box of the provided triangle
7
+ # Calculates a `border` for the bounding box of the given ` triangle`.
8
8
#
9
9
# @param options [Hash]
10
- # @option options x [Float] Point 1 x position.
11
- # @option options y [Float] Point 1 x position.
12
- # @option options x2 [Float] Point 2 x position.
13
- # @option options y2 [Float] Point 2 x position.
14
- # @option options x3 [Float] Point 3 x position.
15
- # @option options y3 [Float] Point 3 x position.
10
+ # @option options x [Float] Vertex 1 x position.
11
+ # @option options y [Float] Vertex 1 y position.
12
+ # @option options x2 [Float] Vertex 2 x position.
13
+ # @option options y2 [Float] Vertex 2 y position.
14
+ # @option options x3 [Float] Vertex 3 x position.
15
+ # @option options y3 [Float] Vertex 3 y position.
16
16
#
17
- # @return [Hash] A DR `border` hash. E.g. `{x: 100, y: 150, w: 300, h: 400}.border!`
17
+ # @return [Hash] A DR `border` hash.
18
18
def triangle_bounding_box ( options = { } )
19
19
x = options [ :x ]
20
20
y = options [ :y ]
@@ -32,17 +32,17 @@ def triangle_bounding_box(options = {})
32
32
{ x : x_min , y : y_min , w : x_max - x_min , h : y_max - y_min } . border!
33
33
end
34
34
35
- # Calculates the center `point` of the provided triangle
35
+ # Calculates the center `point` of the given ` triangle`.
36
36
#
37
37
# @param options [Hash]
38
- # @option options x [Float] Point 1 x position.
39
- # @option options y [Float] Point 1 x position.
40
- # @option options x2 [Float] Point 2 x position.
41
- # @option options y2 [Float] Point 2 x position.
42
- # @option options x3 [Float] Point 3 x position.
43
- # @option options y3 [Float] Point 3 x position.
38
+ # @option options x [Float] Vertex 1 x position.
39
+ # @option options y [Float] Vertex 1 y position.
40
+ # @option options x2 [Float] Vertex 2 x position.
41
+ # @option options y2 [Float] Vertex 2 y position.
42
+ # @option options x3 [Float] Vertex 3 x position.
43
+ # @option options y3 [Float] Vertex 3 y position.
44
44
#
45
- # @return [Hash] A DR `point` hash. E.g. `{x: 20, y: 45}`
45
+ # @return [Hash] A DR `point` hash.
46
46
def triangle_center ( options = { } )
47
47
x = options [ :x ]
48
48
y = options [ :y ]
@@ -54,6 +54,18 @@ def triangle_center(options = {})
54
54
{ x : ( ( x + x2 + x3 ) / 3 ) . to_i , y : ( ( y + y2 + y3 ) / 3 ) . to_i }
55
55
end
56
56
57
+ # Creates the outline of a `circle` .
58
+ #
59
+ # @param options [Hash]
60
+ # @option options x [Float] Center x position.
61
+ # @option options y [Float] Center y position.
62
+ # @option options radius [Float] Radius of the `circle` .
63
+ # @option options r [Integer] Color red value.
64
+ # @option options g [Integer] Color blue value.
65
+ # @option options b [Integer] Color green value.
66
+ # @option options a [Integer] Color alpha value.
67
+ #
68
+ # @return [Array] An array of `solids` in hash notation.
57
69
def circle_outline ( options = { } )
58
70
x = options [ :x ]
59
71
y = options [ :y ]
@@ -85,6 +97,18 @@ def circle_outline(options = {})
85
97
pixels
86
98
end
87
99
100
+ # Create a filled `circle` using raster method.
101
+ #
102
+ # @param options [Hash]
103
+ # @option options x [Float] Center x position.
104
+ # @option options y [Float] Center y position.
105
+ # @option options radius [Float] Radius of the `circle``.
106
+ # @option options r [Integer] Color red value.
107
+ # @option options g [Integer] Color blue value.
108
+ # @option options b [Integer] Color green value.
109
+ # @option options a [Integer] Color alpha value.
110
+ #
111
+ # @return [Array] An array of `solids` in hash notation.
88
112
def circle_raster ( options = { } )
89
113
args = $gtk. args
90
114
@@ -119,6 +143,12 @@ def circle_raster(options = {})
119
143
pixels
120
144
end
121
145
146
+ # Determines if the given `point` is in the given `triangle`.
147
+ #
148
+ # @param point [Hash] `point` in DR hash notation.
149
+ # @param triangle [Hash] `triangle` in Dinraal hash notation.
150
+ #
151
+ # @return [Boolean] `true` or `false`
122
152
def point_inside_triangle? ( point :, triangle :)
123
153
args = $gtk. args
124
154
@@ -139,23 +169,38 @@ def point_inside_triangle?(point:, triangle:)
139
169
leg0_slope = args . geometry . line_slope ( leg0 . flatten , replace_infinity : 1080 )
140
170
leg0_intercept = triangle [ 0 ] [ 1 ] - ( leg0_slope * triangle [ 0 ] [ 0 ] )
141
171
142
- return false unless point_y <= leg0_slope * point_x + leg0_intercept
172
+ return false unless point_y <= ( leg0_slope * point_x ) + leg0_intercept
143
173
144
174
leg1 = [ triangle [ 0 ] , triangle [ 2 ] ]
145
175
leg1_slope = args . geometry . line_slope ( leg1 . flatten , replace_infinity : 1080 )
146
176
leg1_intercept = triangle [ 0 ] [ 1 ] - ( leg1_slope * triangle [ 0 ] [ 0 ] )
147
177
148
- return false unless point_y <= leg1_slope * point_x + leg1_intercept
178
+ return false unless point_y <= ( leg1_slope * point_x ) + leg1_intercept
149
179
150
180
leg2 = [ triangle [ 1 ] , triangle [ 2 ] ]
151
181
leg2_slope = args . geometry . line_slope ( leg2 . flatten , replace_infinity : 1080 )
152
182
leg2_intercept = triangle [ 2 ] [ 1 ] - ( leg2_slope * triangle [ 2 ] [ 0 ] )
153
183
154
- return false unless point_y >= leg2_slope * point_x + leg2_intercept
184
+ return false unless point_y >= ( leg2_slope * point_x ) + leg2_intercept
155
185
156
186
true
157
187
end
158
188
189
+ # Creates the outline of a `triangle`.
190
+ #
191
+ # @param options [Hash]
192
+ # @option options x [Float] Vertex 1 x position.
193
+ # @option options y [Float] Vertex 1 y position.
194
+ # @option options x2 [Float] Vertex 2 x position.
195
+ # @option options y2 [Float] Vertex 2 y position.
196
+ # @option options x3 [Float] Vertex 3 x position.
197
+ # @option options y3 [Float] Vertex 3 y position.
198
+ # @option options r [Integer] Color red value.
199
+ # @option options g [Integer] Color blue value.
200
+ # @option options b [Integer] Color green value.
201
+ # @option options a [Integer] Color alpha value.
202
+ #
203
+ # @return [Array] An array of `solids` in hash notation.
159
204
def triangle_outline ( options = { } )
160
205
x = options [ :x ]
161
206
y = options [ :y ]
@@ -175,6 +220,14 @@ def triangle_outline(options = {})
175
220
lines
176
221
end
177
222
223
+ # Calculates a new `point` given a starting `point`, distance, and angle.
224
+ #
225
+ # @param options [Hash]
226
+ # @option options point [Hash] `point` in DR hash notation.
227
+ # @option options distance [Float] Distance between the given and generated point.
228
+ # @option options angle [Float] Angle from given `point` to generated `point` in degrees.
229
+ #
230
+ # @return [Hash] `point` in DR hash notation.
178
231
def point_at_distance_angle ( options = { } )
179
232
point = options [ :point ]
180
233
distance = options [ :distance ]
@@ -187,6 +240,23 @@ def point_at_distance_angle(options = {})
187
240
new_point
188
241
end
189
242
243
+ # Creates a filled `triangle` using the raster method.
244
+ #
245
+ # @param options [Hash]
246
+ # @option options x [Float] Vertex 1 x position.
247
+ # @option options y [Float] Vertex 1 y position.
248
+ # @option options x2 [Float] Vertex 2 x position.
249
+ # @option options y2 [Float] Vertex 2 y position.
250
+ # @option options x3 [Float] Vertex 3 x position.
251
+ # @option options y3 [Float] Vertex 3 y position.
252
+ # @option options r [Integer] Optional. Color red value. Defaults to `0`.
253
+ # @option options g [Integer] Optional. Color blue value. Defaults to `0`.
254
+ # @option options b [Integer] Optional. Color green value. Defaults to `0`.
255
+ # @option options a [Integer] Optional. Color alpha value. Defaults to `255`.
256
+ # @option options path [String] Optional. Image path. Defaults to `'pixel'`.
257
+ # @option options image_width [Float] Optional. Image width. Defaults to `triangle` width.
258
+ #
259
+ # @return [Array] An array of `solids` in hash notation.
190
260
def triangle_raster ( options = { } )
191
261
x = options [ :x ]
192
262
y = options [ :y ]
@@ -293,6 +363,12 @@ def triangle_raster(options = {})
293
363
raster_lines
294
364
end
295
365
366
+ # Determines if the given `rect` is inside of the given `triangle`.
367
+ #
368
+ # @param rectangle [Hash] `rect` in DR hash notation.
369
+ # @param triangle [Hash] `triangle` in Dinraal hash notation.
370
+ #
371
+ # @return [Boolean] `true` or `false`
296
372
def rectangle_inside_triangle? ( rectangle :, triangle :)
297
373
return false unless point_inside_triangle? ( point : { x : rectangle [ :x ] , y : rectangle [ :y ] } , triangle : triangle )
298
374
@@ -305,6 +381,12 @@ def rectangle_inside_triangle?(rectangle:, triangle:)
305
381
true
306
382
end
307
383
384
+ # Determines if the `inner` `triangle` is contained by the `outer` `triangle`.
385
+ #
386
+ # @param inner [Hash] `triangle` in Dinraal hash notation.
387
+ # @param outer [Hash] `triangle` in Dinraal hash notation.
388
+ #
389
+ # @return [Boolean] `true` or `false`
308
390
def triangle_inside_triangle? ( inner :, outer :)
309
391
# Return true if tri1 is contained by tri2
310
392
return false unless point_inside_triangle? ( point : { x : inner [ :x ] , y : inner [ :y ] } , triangle : outer )
0 commit comments