Skip to content

Commit c73f701

Browse files
committed
Made a circle library
fixed a bug in the triangle code updated the sample app
1 parent d50139c commit c73f701

File tree

7 files changed

+98
-8
lines changed

7 files changed

+98
-8
lines changed

.smaugignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
logs/
2+
builds/
3+
exceptions
4+
Smaug.toml

app/lib/circle.rb

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
module Circle
2+
def raster_outline(options = {})
3+
x = options[:x]
4+
y = options[:y]
5+
radius = options[:radius]
6+
7+
r = options[:r]
8+
g = options[:g]
9+
b = options[:b]
10+
a = options[:a]
11+
12+
pixels = []
13+
14+
angle = 0
15+
while angle < 360
16+
new_pixel = point_at_distance_angle(point: { x: x, y: y }, distance: radius, angle: angle)
17+
18+
new_pixel[:x] = new_pixel[:x].floor
19+
new_pixel[:y] = new_pixel[:y].floor
20+
21+
pixels << { x: new_pixel[:x], y: new_pixel[:y], w: 1, h: 1, r: r, g: g, b: b, a: a }.solid! unless pixels.include?({ x: new_pixel[:x], y: new_pixel[:y], w: 1, h: 1, r: r, g: g, b: b, a: a }.solid!)
22+
angle += 1
23+
end
24+
25+
pixels
26+
end
27+
28+
def raster(options = {})
29+
args = $gtk.args
30+
31+
x = options[:x].floor
32+
y = options[:y].floor
33+
radius = options[:radius]
34+
35+
r = options[:r]
36+
g = options[:g]
37+
b = options[:b]
38+
a = options[:a]
39+
40+
x_min = x - radius.floor
41+
x_max = x + radius.floor
42+
43+
y_min = y - radius.floor
44+
y_max = y + radius.floor
45+
46+
pixels = []
47+
48+
while x_min <= x_max
49+
pixels[x_min] ||= []
50+
y_min = y - radius
51+
while y_min <= y_max
52+
pixels[x_min][y_min] = { x: x_min, y: y_min, w: 1, h: 1, r: r, g: g, b: b, a: a }.solid! if args.geometry.point_inside_circle?({ x: x_min, y: y_min }, { x: x, y: y }, radius)
53+
y_min += 1
54+
end
55+
x_min += 1
56+
end
57+
pixels
58+
end
59+
60+
def point_at_distance_angle(options = {})
61+
point = options[:point]
62+
distance = options[:distance]
63+
angle = options[:angle]
64+
65+
new_point = {}
66+
67+
new_point[:x] = (distance * Math.cos(angle * Math::PI / 180)) + point[:x]
68+
new_point[:y] = (distance * Math.sin(angle * Math::PI / 180)) + point[:y]
69+
new_point
70+
end
71+
end
72+
73+
Circle.extend Circle

app/lib/dinraal.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def bounding_box(options = {})
1616
y_min = [y, y2, y3].min
1717
y_max = [y, y2, y3].max
1818

19-
{ x: x_min, y: y_min, w: x_max - x_min, h: y_max - y_min }
19+
{ x: x_min, y: y_min, w: x_max - x_min, h: y_max - y_min }.border!
2020
end
2121

2222
def center(options = {})

app/main.rb

+17-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
require 'app/lib/dinraal.rb'
2+
require 'app/lib/circle.rb'
3+
4+
$gtk.reset
25

36
def tick(args)
47
# Define two triangles
@@ -8,14 +11,19 @@ def tick(args)
811
# Pre-Render the triangles and their center markers
912
if args.state.tick_count.zero?
1013
args.render_target(:static).primitives << Dinraal.raster(tri1)
14+
15+
args.render_target(:static).primitives << Circle.raster_outline(x: 500, y: 500, radius: 20, g: 255)
16+
17+
args.render_target(:static).primitives << Circle.raster(x: 700, y: 500, radius: 20, b: 255)
1118
end
1219

1320
# Render the triangles and their center markers
14-
args.outputs.sprites << { x: 0, y: 0, w: 1280, h: 720, path: :static }
21+
outputs = []
22+
outputs << { x: 0, y: 0, w: 1280, h: 720, path: :static }.sprite!
1523

16-
args.outputs.primitives << Dinraal.outline(tri2)
17-
args.outputs.primitives << Dinraal.center(tri1).merge(w: 5, h: 5, g: 255, primitive_marker: :solid)
18-
args.outputs.primitives << Dinraal.center(tri2).merge(w: 5, h: 5, g: 255, primitive_marker: :solid)
24+
outputs << Dinraal.outline(tri2)
25+
outputs << Dinraal.center(tri1).merge(w: 5, h: 5, g: 255).solid!
26+
outputs << Dinraal.center(tri2).merge(w: 5, h: 5, g: 255).solid!
1927

2028
# Get the mouse position
2129
mouse_x = args.inputs.mouse.x
@@ -25,12 +33,14 @@ def tick(args)
2533
mouse_inside = Dinraal.inside?(point: { x: mouse_x, y: mouse_y }, tri: tri1)
2634

2735
# Draw a label to display if the mouse inside the red triangle
28-
args.outputs.labels << { x: args.grid.center_x, y: 720, text: "Mouse inside red: #{mouse_inside}", alignment_enum: 1 }
36+
outputs << { x: args.grid.center_x, y: 720, text: "Mouse inside red: #{mouse_inside}", alignment_enum: 1 }.label!
2937

30-
args.outputs.borders << Dinraal.bounding_box(tri2)
38+
outputs << Dinraal.bounding_box(tri2)
3139

3240
two_contains_one = Dinraal.tri_inside?(inner: tri2, outer: tri1)
33-
args.outputs.labels << {x: args.grid.center_x, y: 700, text: "Red contains blue: #{two_contains_one}", alignment_enum: 1}
41+
outputs << { x: args.grid.center_x, y: 700, text: "Red contains blue: #{two_contains_one}", alignment_enum: 1 }.label!
42+
43+
args.outputs.primitives << outputs
3444

3545
# Optional Debug Information. Uncomment to show
3646
# args.outputs.debug << args.gtk.framerate_diagnostics_primitives

logs/log.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
DragonRuby GTK Log Messages. Set $gtk.log_level = :off to disable logging to the Console/HUD (log messages will still be written here). Log options are: :on, :off.
2+
* INFO: Marked app/main.rb for reload. (-1)

logs/puts.txt

Whitespace-only changes.

logs/trace.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add trace!(SOMEOBJECT) to the top of ~tick~ and this file will be populated with invocation information.

0 commit comments

Comments
 (0)