Skip to content

Commit 0fa8c33

Browse files
committed
Create Bounding Box method
1 parent 69be112 commit 0fa8c33

File tree

4 files changed

+64
-19
lines changed

4 files changed

+64
-19
lines changed

Dinraal

-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
{
22
"folders": [
3-
{
4-
"retained": "E64A95092FCA80853577321769BF33F7:Dinraal",
5-
"path": "/Dinraal"
6-
},
73
{
84
"retained": "B8B47FE13002C6AF8FBD52C24767660C:dinraal",
95
"path": "/dinraal"

README.md

+45-15
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,45 @@ def tick args
1515
end
1616
```
1717

18-
The library source file can be found at [https://github.com/DSchaedler/dinraal/blob/main/app/lib/dinraal.rb](https://github.com/DSchaedler/dinraal/blob/main/app/lib/dinraal.rb).
19-
(app/lib/dinraal.rb)
18+
The library source file can be found at [app/lib/dinraal.rb](https://github.com/DSchaedler/dinraal/blob/main/app/lib/dinraal.rb).
2019

21-
A more detailed sample app is at [https://github.com/DSchaedler/dinraal/blob/main/app/main.rb](https://github.com/DSchaedler/dinraal/blob/main/app/main.rb).
22-
(app/main.rb)
20+
A more detailed sample app is at [app/main.rb](https://github.com/DSchaedler/dinraal/blob/main/app/main.rb).
2321

2422
# Common Parameters
2523

2624
All methods in Dinraal accept their options in hash notation. This allows for extra or missing options to be ignored or inferred.
2725

2826
## Usually Required
29-
x = (int, float) x position of point 1 on the triangle.
30-
y = (int, float) y position of point 1 on the triangle.
31-
x2 = (int, float) x position of point 2 on the triangle.
32-
y2 = (int, float) y position of point 2 on the triangle.
33-
x3 = (int, float) x position of point 3 on the triangle.
34-
y3 = (int, float) y position of point 3 on the triangle.
27+
```
28+
x = (int, float) x position of point 1 on the triangle.
29+
y = (int, float) y position of point 1 on the triangle.
30+
x2 = (int, float) x position of point 2 on the triangle.
31+
y2 = (int, float) y position of point 2 on the triangle.
32+
x3 = (int, float) x position of point 3 on the triangle.
33+
y3 = (int, float) y position of point 3 on the triangle.
34+
```
3535

3636
## Usually Optional
37-
r = (int, float) red portion of the triangle's color.
38-
g = (int, float) green portion of the triangle's color.
39-
b = (int, float) blue portion of the triangle's color.
40-
a = (int, float) alpha portion of the triangle's color.
37+
```
38+
r = (int, float) red portion of the triangle's color.
39+
g = (int, float) green portion of the triangle's color.
40+
b = (int, float) blue portion of the triangle's color.
41+
a = (int, float) alpha portion of the triangle's color.
42+
```
4143

4244
# Methods
4345

46+
## `bounding_box`
47+
```ruby
48+
Dinraal.bounding_box( { x: point1_x, y: point1_y, x2: point2_x, y2: point2_y, x3: point3_x, y3: point3_y } )
49+
```
50+
51+
Returns a rectangle that contains the given triangle.
52+
53+
```ruby
54+
{ x: x, y: y, w: width, h: height }
55+
```
56+
4457
## `center`
4558
```ruby
4659
Dinraal.center( { x: point1_x, y: point1_y, x2: point2_x, y2: point2_y, x3: point3_x, y3: point3_y } )
@@ -96,4 +109,21 @@ It is recommended to call this method and cache it's result, as it will lag with
96109

97110
# TODO
98111

99-
- Create `bounding_box` method.
112+
- Create `tri_intersects?` method
113+
- Create `rect_intersects?` method
114+
- Create `tri_inside?` method
115+
- Create `rect_inside?` method
116+
- Create `inradius` method - [Incircle Math Breakdown](https://artofproblemsolving.com/wiki/index.php/Incircle)
117+
- Create `area` method - [Heron's Formula](https://artofproblemsolving.com/wiki/index.php/Heron%27s_Formula)
118+
- - Vector based calculations will be faster, but less straightforward to impliment
119+
- Create `perimiter` method
120+
-
121+
122+
# How to contribute
123+
- Submit a Pull Request with any code improvements
124+
- Impliment any item from the TODO list.
125+
- Write a sample app that more clearly shows all methods
126+
- Write a tutorial
127+
- Submit improved calculation methods
128+
- - Speed improvements to the `raster` method
129+
- - Vector calculations for area over Heron's Formula

app/lib/dinraal.rb

+17
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,23 @@
22
# By D Schaedler. Released under MIT License.
33
# https://github.com/DSchaedler/dinraal
44
module Dinraal
5+
def bounding_box(options = {})
6+
x = options[:x]
7+
y = options[:y]
8+
x2 = options[:x2]
9+
y2 = options[:y2]
10+
x3 = options[:x3]
11+
y3 = options[:y3]
12+
13+
x_min = [x, x2, x3].min
14+
x_max = [x, x2, x3].max
15+
16+
y_min = [y, y2, y3].min
17+
y_max = [y, y2, y3].max
18+
19+
{ x: x_min, y: y_min, w: x_max - x_min, h: y_max - y_min }
20+
end
21+
522
def center(options = {})
623
x = options[:x]
724
y = options[:y]

app/main.rb

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ def tick(args)
2626
# Draw a label to display if the mouse inside the red triangle
2727
args.outputs.labels << { x: args.grid.center_x, y: 720, text: "Mouse inside red: #{mouse_inside}", alignment_enum: 1 }
2828

29+
args.outputs.borders << Dinraal.bounding_box(tri2)
30+
2931
# Optional Debug Information. Uncomment to show
3032
# args.outputs.debug << args.gtk.framerate_diagnostics_primitives
3133
end

0 commit comments

Comments
 (0)