Difference between revisions of "Branch Assignment"

From CSE425S Wiki
Jump to navigation Jump to search
Line 3: Line 3:
  
 
=Code to Investigate=
 
=Code to Investigate=
  <nowiki>class Turtle
+
==LogoTurtle==
   def self.set_color(color)
+
  <nowiki>class LogoTurtle
     glColor3f(color.red, color.green, color.blue)
+
  attr_accessor :pen_color, :pen_width
 +
   def initialize
 +
    super
 +
    @is_pen_down = true
 +
    @pen_width = 0.005
 +
     @pen_color = Color::WHITE
 
   end
 
   end
  
   def self.draw_line(length, width_in_pixels)
+
   def pen_up
     glLineWidth(width_in_pixels)
+
     @is_pen_down = true
    glBegin(GL_LINES)
 
    glVertex2d(0.0, 0.0)
 
    glVertex2d(length, 0.0)
 
    glEnd()
 
 
   end
 
   end
  
   def self.move_forward(magnitude)
+
   def pen_down
     glTranslatef(magnitude, 0.0, 0.0)
+
     @is_pen_down = false
 
   end
 
   end
  
   def self.turn(theta)
+
 
    glRotatef(theta, 0.0, 0.0, 1.0)
+
   def forward(amount)
 +
    y = amount
 +
    x = @pen_width
 +
    if @is_pen_down
 +
      glColor3f(@pen_color.red, @pen_color.green, @pen_color.blue)
 +
      glBegin(GL_QUADS)
 +
      glVertex2d(-x, 0.0)
 +
      glVertex2d(-x, y)
 +
      glVertex2d(x, y)
 +
      glVertex2d(x, 0.0)
 +
      glEnd()
 +
    end
 +
    glTranslatef(0,y,0)
 +
  end
 +
 
 +
  def right(degrees)
 +
    glRotatef(degrees, 0, 0, 1)
 +
  end
 +
end
 +
</nowiki>
 +
==Flower Client==
 +
<nowiki>class Flower
 +
  def initialize
 +
    super
 +
    @turtle = LogoTurtle.new
 +
  end
 +
 
 +
  def render
 +
    flower
 +
  end
 +
 
 +
  private
 +
 
 +
  def square
 +
    4.times do
 +
      @turtle.forward 0.5
 +
      @turtle.right 90
 +
    end
 +
  end
 +
 
 +
  def flower
 +
    36.times do
 +
      @turtle.right 10
 +
      square
 +
    end
 
   end
 
   end
 
end</nowiki>
 
end</nowiki>

Revision as of 14:44, 20 April 2022

Lindenmayer system

Dragon trees.jpg

Code to Investigate

LogoTurtle

class LogoTurtle
  attr_accessor :pen_color, :pen_width
  def initialize
    super
    @is_pen_down = true
    @pen_width = 0.005
    @pen_color = Color::WHITE
  end

  def pen_up
    @is_pen_down = true
  end

  def pen_down
    @is_pen_down = false
  end


  def forward(amount)
    y = amount
    x = @pen_width
    if @is_pen_down
      glColor3f(@pen_color.red, @pen_color.green, @pen_color.blue)
      glBegin(GL_QUADS)
      glVertex2d(-x, 0.0)
      glVertex2d(-x, y)
      glVertex2d(x, y)
      glVertex2d(x, 0.0)
      glEnd()
    end
    glTranslatef(0,y,0)
  end

  def right(degrees)
    glRotatef(degrees, 0, 0, 1)
  end
end

Flower Client

class Flower
  def initialize
    super
    @turtle = LogoTurtle.new
  end

  def render
    flower
  end

  private

  def square
    4.times do
      @turtle.forward 0.5
      @turtle.right 90
    end
  end

  def flower
    36.times do
      @turtle.right 10
      square
    end
  end
end

Code to Implement

OpenGLStackUtils

class OpenGLStackUtils
  def self.push_do_pop
    raise :not_yet_implemented
  end
end

Branch

class Branch
  def branch(length, line_width_in_pixels, depth_remaining)
    raise :not_yet_implemented
  end
end

Note: to produce the reference images the code below was used:

      next_length = length * 0.5
      next_line_width_in_pixels = line_width_in_pixels * 0.8

Note: the child branches are 1.0/3.0 from the beginning and the end.

Note: the child branches are rotated 30.0 degrees and -30.0 degrees.

Note: in the recursive case the child branches were drawn before the line at the current depth to allow the green to be beneath the brown.

Do NOT worry if your images are not exact matches of the reference images.

Branch MaxDepth0.png Branch MaxDepth1.png Branch MaxDepth2.png Branch MaxDepth3.png Branch MaxDepth4.png Branch MaxDepth5.png
max_depth: 0 max_depth: 1 max_depth: 2 max_depth: 3 max_depth: 4 max_depth: 5