Difference between revisions of "Render Part D Assignment"

From CSE425S Wiki
Jump to navigation Jump to search
Line 13: Line 13:
 
=Code to Implement=
 
=Code to Implement=
 
==Composite==
 
==Composite==
{{RubyModuleToImplement|composite|render/assignment|Composite|initialize_components()<br/>push_component(component)<br/>each_component()}}
+
{{RubyModuleToImplement|composite|render/assignment|Composite|initialize_components()<br/>push_component(component)<br/>each_component()<br/>calculate_local_bounds()}}
  
 
module Composite should define methods <code>initialize_components</code>, <code>push_component</code>, and <code>each_component</code> as well as mixin the [https://ruby-doc.org/core-2.6.3/Enumerable.html Enumerable] module.
 
module Composite should define methods <code>initialize_components</code>, <code>push_component</code>, and <code>each_component</code> as well as mixin the [https://ruby-doc.org/core-2.6.3/Enumerable.html Enumerable] module.
Line 31: Line 31:
  
 
Note: be sure to invoke <code>to_enum</code> with the symbol of the method <code>:each_component</code> and not invoke the method by mistake.
 
Note: be sure to invoke <code>to_enum</code> with the symbol of the method <code>:each_component</code> and not invoke the method by mistake.
 +
 +
===calculate_local_bounds===
 +
Similar to the bounds calculations on CompositeTransform and Scene from Part C.
  
 
===Mixin Enumerable===
 
===Mixin Enumerable===

Revision as of 09:23, 9 December 2020

In this studio we will evolve our code from Render_Part_C_Assignment to leverage mixins.

Render part d class hierarchy.svg

Continue editing files in the render/assignment directory.

Background

Investigate Ruby Modules, specifically the module and include keywords.

Warning

caption

Do NOT use the include keyword in Ruby as you would typically in C or C++.
Be sure to include within the intended class or module definition.
If you include at the top-level, it will mixin to Object!
This may or may not rip a hole in the universe.

Code to Implement

Composite

file: src/main/ruby/render/assignment/composite.rb Ruby logo.svg
module: Composite
methods: initialize_components()
push_component(component)
each_component()
calculate_local_bounds()

module Composite should define methods initialize_components, push_component, and each_component as well as mixin the Enumerable module.

initialize_components

add an instance variable with a suitable name to store an array of components

NOTE: initialize_components() must be private.

push_component

push a component onto your array instance variable.

each_component

if a block is given, yield for each component in your array instance variable.

otherwise use to_enum to return a new Enumerator of the components.

Note: be sure to invoke to_enum with the symbol of the method :each_component and not invoke the method by mistake.

calculate_local_bounds

Similar to the bounds calculations on CompositeTransform and Scene from Part C.

Mixin Enumerable

to add a number of useful methods, include the Enumerable module and

Alias each

use alias_method to alias an :each method to the :each_component method.

CompositeTransform

Mixin your Composite module to the CompositeTransform class.

include Composite

Be sure to invoke initialize_components from the constructor and clean up existing code. For example bounds calculation can now use map.

Scene

Mixin your Composite module to the Scene class.

include Composite

Be sure to invoke initialize_components from the constructor and clean up existing code. For example bounds calculation can now use map.

Testing Your Solution

Unit Test

Ensure that all of the tests in the src/test/ruby/render/part_d directory work.

Additionally, ensure that you are backwards compatible by checking the previous render studio tests.