Difference between revisions of "Render Part B Assignment"

From CSE425S Wiki
Jump to navigation Jump to search
Line 15: Line 15:
 
Transform's constructor should take x and y parameters and store them in instance variables.
 
Transform's constructor should take x and y parameters and store them in instance variables.
  
It should define a render method which [https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glPushMatrix.xml preserves the current model view transformation], [https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glTranslate.xml translates in the z==0 plane], passes the render_transformed message to itself (a.k.a. invokes a method on itself), and [https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glPopMatrix.xml restores the model view transformation].
+
It should define a render method which [https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glPushMatrix.xml preserves the current model view transformation], [https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glTranslate.xml translates in the z==0 plane], passes the untransformed_render message to itself (a.k.a. invokes a method on itself), and [https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glPopMatrix.xml restores the model view transformation].
  
All subclasses must implement a private render_transformed method.
+
All subclasses must implement a private untransformed_render method.
  
 
==Image==
 
==Image==
{{RubyToEvolve|image|drawings|Image|Transform|initialize(path, x: 0, y: 0)|render_transformed()|render()}}
+
{{RubyToEvolve|image|drawings|Image|Transform|initialize(path, x: 0, y: 0)|untransformed_render()|render()}}
  
 
You will evolve your code from [[Render_Part_A_Assignment#Image|from Part A]]
 
You will evolve your code from [[Render_Part_A_Assignment#Image|from Part A]]
Line 30: Line 30:
 
Add keyword parameters for x: 0 and y: 0 and invoke your superclass with these parameters.
 
Add keyword parameters for x: 0 and y: 0 and invoke your superclass with these parameters.
  
NOTE: Be sure to not override render.  Implement the render_transformed method instead.
+
NOTE: Be sure to not override render.  Implement the untransformed_render method instead.
  
 
NOTE: render_transformed() must be private.
 
NOTE: render_transformed() must be private.
Line 46: Line 46:
  
 
==Text==
 
==Text==
{{RubyToEvolve|text|drawings|Text|ColorTransform|initialize(text, font,  x: 0, y: 0, color: nil)|render_transformed()|render()}}
+
{{RubyToEvolve|text|drawings|Text|ColorTransform|initialize(text, font,  x: 0, y: 0, color: nil)|untransformed_render()|render()}}
  
 
You will evolve your code from [[Render_Part_A_Assignment#Text|from Part A]]
 
You will evolve your code from [[Render_Part_A_Assignment#Text|from Part A]]
Line 56: Line 56:
 
Add keyword parameters for x: 0, y: 0, and color: nil to the constructor and pass them to the superclass.
 
Add keyword parameters for x: 0, y: 0, and color: nil to the constructor and pass them to the superclass.
  
NOTE: Be sure to not override render.  Implement the render_transformed method instead.
+
NOTE: Be sure to not override render.  Implement the untransformed_render method instead.
  
 
NOTE: render_transformed() must be private.
 
NOTE: render_transformed() must be private.
  
 
==Ellipse==
 
==Ellipse==
{{RubyToEvolve|ellipse|drawings|Ellipse|ColorTransform|initialize(x_radius, y_radius, x: 0, y: 0, color: nil)|render_transformed()|render()}}
+
{{RubyToEvolve|ellipse|drawings|Ellipse|ColorTransform|initialize(x_radius, y_radius, x: 0, y: 0, color: nil)|untransformed_render()|render()}}
  
 
You will evolve your code from [[Render_Part_A_Assignment#Ellipse|from Part A]]
 
You will evolve your code from [[Render_Part_A_Assignment#Ellipse|from Part A]]
Line 71: Line 71:
 
Add keyword parameters for x: 0, y: 0, and color: nil to the constructor and pass them to the superclass.
 
Add keyword parameters for x: 0, y: 0, and color: nil to the constructor and pass them to the superclass.
  
NOTE: Be sure to not override render.  Implement the render_transformed method instead.
+
NOTE: Be sure to not override render.  Implement the untransformed_render method instead.
  
 
NOTE: render_transformed() must be private.
 
NOTE: render_transformed() must be private.
  
 
==EquilateralTriangle==
 
==EquilateralTriangle==
{{RubyToEvolve|equilateral_triangle|drawings|EquilateralTriangle|ColorTransform|initialize(half_side_length, x: 0, y: 0, color: nil)|render_transformed()|render()}}
+
{{RubyToEvolve|equilateral_triangle|drawings|EquilateralTriangle|ColorTransform|initialize(half_side_length, x: 0, y: 0, color: nil)|untransformed_render()|render()}}
  
 
You will evolve your code from [[Render_Part_A_Assignment#EquilateralTriangle|from Part A]]
 
You will evolve your code from [[Render_Part_A_Assignment#EquilateralTriangle|from Part A]]
Line 86: Line 86:
 
Add keyword parameters for x: 0, y: 0, and color: nil to the constructor and pass them to the superclass.
 
Add keyword parameters for x: 0, y: 0, and color: nil to the constructor and pass them to the superclass.
  
NOTE: Be sure to not override render.  Implement the render_transformed method instead.
+
NOTE: Be sure to not override render.  Implement the untransformed_render method instead.
  
 
NOTE: render_transformed() must be private.
 
NOTE: render_transformed() must be private.
  
 
==BezierCurve==
 
==BezierCurve==
{{RubyToEvolve|bezier_curve|drawings|BezierCurve|ColorTransform|initialize(control_points, x: 0, y: 0, color: nil)|render_transformed()|render()}}
+
{{RubyToEvolve|bezier_curve|drawings|BezierCurve|ColorTransform|initialize(control_points, x: 0, y: 0, color: nil)|untransformed_render()|render()}}
  
 
You will evolve your code from [[Render_Part_A_Assignment#B.C3.A9zier_Curve|from Part A]]
 
You will evolve your code from [[Render_Part_A_Assignment#B.C3.A9zier_Curve|from Part A]]
Line 101: Line 101:
 
Add keyword parameters for x: 0, y: 0, and color: nil to the constructor and pass them to the superclass.
 
Add keyword parameters for x: 0, y: 0, and color: nil to the constructor and pass them to the superclass.
  
NOTE: Be sure to not override render.  Implement the render_transformed method instead.
+
NOTE: Be sure to not override render.  Implement the untransformed_render method instead.
  
 
NOTE: render_transformed() must be private.
 
NOTE: render_transformed() must be private.
  
 
==CircularSegment==
 
==CircularSegment==
{{RubyToEvolve|circular_segment|drawings|CircularSegment|ColorTransform|initialize(x_radius, y_radius, theta_a, theta_z, x: 0, y: 0, color: nil)|render_transformed()|render()}}
+
{{RubyToEvolve|circular_segment|drawings|CircularSegment|ColorTransform|initialize(x_radius, y_radius, theta_a, theta_z, x: 0, y: 0, color: nil)|untransformed_render()|render()}}
  
 
You will evolve your code from [[Render_Part_A_Assignment#CircularSegment|from Part A]]
 
You will evolve your code from [[Render_Part_A_Assignment#CircularSegment|from Part A]]
Line 116: Line 116:
 
Add keyword parameters for x: 0, y: 0, and color: nil to the constructor and pass them to the superclass.
 
Add keyword parameters for x: 0, y: 0, and color: nil to the constructor and pass them to the superclass.
  
NOTE: Be sure to not override render.  Implement the render_transformed method instead.
+
NOTE: Be sure to not override render.  Implement the untransformed_render method instead.
  
 
NOTE: render_transformed() must be private.
 
NOTE: render_transformed() must be private.
  
 
==Rectangle==
 
==Rectangle==
{{RubyToEvolve|rectangle|drawings|Rectangle|ColorTransform|initialize(half_width, half_height, x: 0, y: 0, color: nil)|render_transformed()|render()}}
+
{{RubyToEvolve|rectangle|drawings|Rectangle|ColorTransform|initialize(half_width, half_height, x: 0, y: 0, color: nil)|untransformed_render()|render()}}
  
 
You will evolve your code from [[Render_Part_A_Assignment#Rectangle|from Part A]]
 
You will evolve your code from [[Render_Part_A_Assignment#Rectangle|from Part A]]
Line 131: Line 131:
 
Add keyword parameters for x: 0, y: 0, and color: nil to the constructor and pass them to the superclass.
 
Add keyword parameters for x: 0, y: 0, and color: nil to the constructor and pass them to the superclass.
  
NOTE: Be sure to not override render.  Implement the render_transformed method instead.
+
NOTE: Be sure to not override render.  Implement the untransformed_render method instead.
  
 
NOTE: render_transformed() must be private.
 
NOTE: render_transformed() must be private.
  
 
==CompositeTransform==
 
==CompositeTransform==
{{RubyToImplement|composite_transform|drawings|CompositeTransform|Transform|initialize(x: 0, y: 0)<br/>components<br/>push_component(component)<br/>render_transformed()}}
+
{{RubyToImplement|composite_transform|drawings|CompositeTransform|Transform|initialize(x: 0, y: 0)<br/>components<br/>push_component(component)<br/>untransformed_render()}}
  
 
Add a reader for components.
 
Add a reader for components.
Line 144: Line 144:
 
The render_transformed method should render each pushed component.
 
The render_transformed method should render each pushed component.
  
NOTE: render_transformed() must be private.
+
NOTE: untransformed_render() must be private.
  
 
NOTE: sometimes when constructing objects we need to initialize instance variables that are not necessarily passed in as arguments.  this is one of those times.
 
NOTE: sometimes when constructing objects we need to initialize instance variables that are not necessarily passed in as arguments.  this is one of those times.

Revision as of 06:09, 21 November 2022

In this studio we will evolve our code from Render (Part A) to leverage class hierarchies.

Render part b class hierarchy.svg

Continue editing files in the drawings directory.

In this studio, we will have parameters for which explicitly passed arguments are not required (for convenience and backwards compatibility). We will prefer keyword arguments over optional arguments.

Code to Implement

Transform

file: src/main/ruby/drawings/transform.rb Ruby logo.svg
class: Transform
superclass: Object
methods: initialize(x,y)
x()
x=()
y()
y=()
render()

Add accesors for x and y.

Transform's constructor should take x and y parameters and store them in instance variables.

It should define a render method which preserves the current model view transformation, translates in the z==0 plane, passes the untransformed_render message to itself (a.k.a. invokes a method on itself), and restores the model view transformation.

All subclasses must implement a private untransformed_render method.

Image

file: src/main/ruby/drawings/image.rb Ruby logo.svg
class: Image
superclass: Transform
methods to evolve: initialize(path, x: 0, y: 0)
methods to add: untransformed_render()
methods to remove: render()

You will evolve your code from from Part A

be sure to add the following line above your class:

require_relative 'transform'

Add keyword parameters for x: 0 and y: 0 and invoke your superclass with these parameters.

NOTE: Be sure to not override render. Implement the untransformed_render method instead.

NOTE: render_transformed() must be private.

ColorTransform

file: src/main/ruby/drawings/color_transform.rb Ruby logo.svg
class: ColorTransform
superclass: Transform
methods: initialize(x,y,color)
color()
color=()
render()

Add an accessor for color.

ColorTransform's constructor should take x, y, and color parameters. x and y should be passed to the superclass and color should be stored in an instance variable.

NOTE: Override render. Be sure to change the color (unless it is nil) before invoking your superclass.

NOTE: invoke the nil? method to check for nil.

Text

file: src/main/ruby/drawings/text.rb Ruby logo.svg
class: Text
superclass: ColorTransform
methods to evolve: initialize(text, font, x: 0, y: 0, color: nil)
methods to add: untransformed_render()
methods to remove: render()

You will evolve your code from from Part A

be sure to add the following line above your class:

require_relative 'color_transform'

Add keyword parameters for x: 0, y: 0, and color: nil to the constructor and pass them to the superclass.

NOTE: Be sure to not override render. Implement the untransformed_render method instead.

NOTE: render_transformed() must be private.

Ellipse

file: src/main/ruby/drawings/ellipse.rb Ruby logo.svg
class: Ellipse
superclass: ColorTransform
methods to evolve: initialize(x_radius, y_radius, x: 0, y: 0, color: nil)
methods to add: untransformed_render()
methods to remove: render()

You will evolve your code from from Part A

be sure to add the following line above your class:

require_relative 'color_transform'

Add keyword parameters for x: 0, y: 0, and color: nil to the constructor and pass them to the superclass.

NOTE: Be sure to not override render. Implement the untransformed_render method instead.

NOTE: render_transformed() must be private.

EquilateralTriangle

file: src/main/ruby/drawings/equilateral_triangle.rb Ruby logo.svg
class: EquilateralTriangle
superclass: ColorTransform
methods to evolve: initialize(half_side_length, x: 0, y: 0, color: nil)
methods to add: untransformed_render()
methods to remove: render()

You will evolve your code from from Part A

be sure to add the following line above your class:

require_relative 'color_transform'

Add keyword parameters for x: 0, y: 0, and color: nil to the constructor and pass them to the superclass.

NOTE: Be sure to not override render. Implement the untransformed_render method instead.

NOTE: render_transformed() must be private.

BezierCurve

file: src/main/ruby/drawings/bezier_curve.rb Ruby logo.svg
class: BezierCurve
superclass: ColorTransform
methods to evolve: initialize(control_points, x: 0, y: 0, color: nil)
methods to add: untransformed_render()
methods to remove: render()

You will evolve your code from from Part A

be sure to add the following line above your class:

require_relative 'color_transform'

Add keyword parameters for x: 0, y: 0, and color: nil to the constructor and pass them to the superclass.

NOTE: Be sure to not override render. Implement the untransformed_render method instead.

NOTE: render_transformed() must be private.

CircularSegment

file: src/main/ruby/drawings/circular_segment.rb Ruby logo.svg
class: CircularSegment
superclass: ColorTransform
methods to evolve: initialize(x_radius, y_radius, theta_a, theta_z, x: 0, y: 0, color: nil)
methods to add: untransformed_render()
methods to remove: render()

You will evolve your code from from Part A

be sure to add the following line above your class:

require_relative 'color_transform'

Add keyword parameters for x: 0, y: 0, and color: nil to the constructor and pass them to the superclass.

NOTE: Be sure to not override render. Implement the untransformed_render method instead.

NOTE: render_transformed() must be private.

Rectangle

file: src/main/ruby/drawings/rectangle.rb Ruby logo.svg
class: Rectangle
superclass: ColorTransform
methods to evolve: initialize(half_width, half_height, x: 0, y: 0, color: nil)
methods to add: untransformed_render()
methods to remove: render()

You will evolve your code from from Part A

be sure to add the following line above your class:

require_relative 'color_transform'

Add keyword parameters for x: 0, y: 0, and color: nil to the constructor and pass them to the superclass.

NOTE: Be sure to not override render. Implement the untransformed_render method instead.

NOTE: render_transformed() must be private.

CompositeTransform

file: src/main/ruby/drawings/composite_transform.rb Ruby logo.svg
class: CompositeTransform
superclass: Transform
methods: initialize(x: 0, y: 0)
components
push_component(component)
untransformed_render()

Add a reader for components.

Add a push_component method which accepts a component which will later be rendered.

The render_transformed method should render each pushed component.

NOTE: untransformed_render() must be private.

NOTE: sometimes when constructing objects we need to initialize instance variables that are not necessarily passed in as arguments. this is one of those times.

Scene

file: src/main/ruby/drawings/scene.rb Ruby logo.svg
class: Scene
superclass: Object
methods: initialize(background_color: Color::BLACK)
background_color()
background_color=()
components()
push_component(component)
render()

Add an accessor for background_color and a reader for components.

Scene's constructor should take a keyword parameter background_color: Color.new(0,0,0) and store it in an instance variable.

Add a push_component method which accepts a component which will later be rendered.

The render method should set the clear color and clear the color buffer before rendering each pushed component.

glClearColor(@background_color.red, @background_color.green, @background_color.blue, 1.0)
glClear(GL_COLOR_BUFFER_BIT)

Testing Your Solution

Unit Test

file: src/test/ruby/drawings/preliminary/part_b/*.rb Ruby logo.svg UnitTest

note: ensure that you have removed all printing to receive credit for any assignment.

file: src/test/ruby/drawings/more_comprehensive/part_b/*.rb Ruby logo.svg UnitTest

note: ensure that you have removed all printing to receive credit for any assignment.

Visual Comparison

file to run: src/main/ruby/drawings_snapshots/part_b_snapshots_web_page_generator.rb Ruby logo.svg

Somewhat Outdated Testing Demo