# saucer.rb
include Math
class Saucer
include Processing::Proxy
# include processing as Mixin
R1 = 4.0 # class constants that together
R2 = 3.0 # with PI constants are used
R3 = 6.0 # to define the Saucer shape
A1 = 0.25
A2 = 0.60
A3 = 0.0
MAX_THETA = QUARTER_PI/2
attr_reader :xpos, :ypos, :my_scale, :tilt
def initialize xpos, ypos
@xpos = xpos
@ypos = ypos
@my_scale = 10.0
@tilt = 0
end
def get_point radius, theta
x = xpos + (radius * Math.cos(theta + tilt)) *my_scale
y = ypos + (radius * Math.sin(theta + tilt)) *my_scale
return x, y
end
def set_tilt(theta)
theta = Saucer::MAX_THETA if theta > Saucer::MAX_THETA
theta = -Saucer::MAX_THETA if theta < -Saucer::MAX_THETA
@tilt = theta
end
def set_scale(scale)
@my_scale = scale unless scale < 0
end
def render
smooth
curve_tightness -0.05
begin_shape
x, y = get_point(Saucer::R1, Saucer::A1)
curve_vertex(x, y)
x, y = get_point(Saucer::R2, TWO_PI - Saucer::A2)
curve_vertex(x, y)
x, y = get_point(Saucer::R2, PI + Saucer::A2)
curve_vertex(x, y)
x, y = get_point(Saucer::R1, PI - Saucer::A1)
curve_vertex(x, y)
x, y = get_point(Saucer::R2, PI + Saucer::A2)
vertex(x, y)
x, y = get_point(Saucer::R3, PI)
vertex(x, y)
vertex(x, y)
x, y = get_point(Saucer::R1, PI - Saucer::A1)
vertex(x, y)
vertex(x, y)
x, y = get_point(Saucer::R1, Saucer::A1)
vertex(x, y)
vertex(x, y)
x, y = get_point(Saucer::R3, Saucer::A3)
vertex(x, y)
vertex(x, y)
end_shape CLOSE
end
end
Here is the main code:-
require 'saucer'
class MixinsTest < Processing::App
def setup
@saucer = Saucer.new 70, 50
@saucer.set_tilt QUARTER_PI/2
@saucer1 = Saucer.new 130, 170
@saucer1.set_scale 15
@saucer1.set_tilt QUARTER_PI/3
@saucer2 = Saucer.new 210, 340
@saucer2.set_scale 25
end
def draw
background 11, 17, 67
fill 116, 39, 39
@saucer.render
fill 255, 0, 0
@saucer1.render
@saucer2.render
save_frame("saucer.png")
end
end
MixinsTest.new :width => 400, :height => 400, :title => "Inner Test"
Heres the result of running the code 'rp5 run main.rb'

No comments:
Post a Comment