Difference between revisions of "W1503 Lines and Ellipses"

From Coder Merlin
 
(23 intermediate revisions by 2 users not shown)
Line 1: Line 1:
= Prerequisites =
[[File:Back-of-dollar-1.jpg|thumb|link=|Dollar Bill]]
== Prerequisites ==
* [[W1502 Object Attributes]]
* [[W1502 Object Attributes]]


= Research =  
== Research ==  
* Examine the back of a dollar bill.  Note the pyramid on the left-hand side.
* Examine the back of a dollar bill.  Note the pyramid on the left-hand side.
== Prepare ==
{{ScenesShellPrepare|W1503}}


= Experiment =
== Experiment ==
{{StopProgram|Stop the running program.
Return to the ''console'' and press {{SpecialKey|CONTROL|C}}
}}


Enter into the Sources directory of the IgisShell-Introduction project.
=== Render a Line ===
<syntaxhighlight lang="bash">
Open the file {{Pathname|Background.swift}} in emacs.
cd ~/projects/IgisShell-Introduction/Sources/IgisShell/
</syntaxhighlight>
 
Open main.swift in emacs.
<syntaxhighlight lang="bash">
emacs main.swift
</syntaxhighlight>


== Paint a Single Blue Line ==
Add a new method (below ''init'') as follows:
Find the function within the Painter object named "setup".  Edit the contents as follows:
<syntaxhighlight lang="swift">
<syntaxhighlight lang="swift">
     func setup(canvas:Canvas) {
     override func setup(canvasSize:Size, canvas:Canvas) {
         let lineWidth = LineWidth(width:3)
         let lineWidth = LineWidth(width:3)
         canvas.paint(lineWidth)
         canvas.render(lineWidth)


         let blue = StrokeStyle(color:Color(red:100, green:100, blue:255))
         let blue = StrokeStyle(color:Color(red:100, green:100, blue:255))
         canvas.paint(blue)
         canvas.render(blue)


         let lines = Lines(from:Point(x:10, y:50), to:Point(x:100, y:50))
         let lines = Lines(from:Point(x:110, y:150), to:Point(x:200, y:150))
         canvas.paint(lines)
         canvas.render(lines)
     }
     }
</syntaxhighlight>
</syntaxhighlight>


== Paint a Triangle ==
Remember to save the file, then suspend emacs.
Find the function within the Painter object named "setup"Edit the contents as follows:
 
{{Hint|
An online tool to determine color values is available [https://htmlcolorcodes.com/color-picker/ here]Use the RGB values in the following constructor to generate any custom color:
<syntaxhighlight lang="swift">
<syntaxhighlight lang="swift">
     func setup(canvas:Canvas) {
Color(red:UInt8, green:UInt8, blue:UInt8)
</syntaxhighlight>
}}
 
 
{{RunProgram|Run the program and refresh the browser page.}}
 
 
{{Observe|: Section 1|
# What do you observe?
# What is the purpose of <syntaxhighlight lang="swift" inline>LineWidth</syntaxhighlight>?
# Why do we draw lines with <syntaxhighlight lang="swift" inline>StrokeStyle</syntaxhighlight> rather than <syntaxhighlight lang="swift" inline>FillStyle</syntaxhighlight>?
}}
 
 
{{StopProgram|Stop the running program.}}
 
=== Render a Triangle ===
Resume emacs and edit the setup function so it appears as follows:
<syntaxhighlight lang="swift" highlight="9-10">
     override func setup(canvasSize:Size, canvas:Canvas) {
         let lineWidth = LineWidth(width:3)
         let lineWidth = LineWidth(width:3)
         canvas.paint(lineWidth)
         canvas.render(lineWidth)


         let blue = StrokeStyle(color:Color(red:100, green:100, blue:255))
         let blue = StrokeStyle(color:Color(red:100, green:100, blue:255))
         canvas.paint(blue)
         canvas.render(blue)


         let lines = Lines(from:Point(x:10, y:50), to:Point(x:100, y:50))
         let lines = Lines(from:Point(x:110, y:150), to:Point(x:200, y:150))
         lines.lineTo(Point(x:55, y:150))
         lines.lineTo(Point(x:155, y:250))
         lines.lineTo(Point(x:10, y:50))
         lines.lineTo(Point(x:110, y:150))
         canvas.paint(lines)
         canvas.render(lines)
     }
     }
</syntaxhighlight>
</syntaxhighlight>


Now, run the program and view in a browser before continuing.
{{RunProgram|Run the program and refresh the browser page.}}


== Circumscribe the Triangle with an Ellipse ==
 
Find the function within the Painter object named "setup".  Edit the contents as follows:
{{Observe|: Section 2|
<syntaxhighlight lang="swift">
# What do you observe?
     func setup(canvas:Canvas) {
# Which statements draw the triangle?
# What would happen without line 10 (in the above listing)?
}}
 
 
{{StopProgram|Stop the running program.}}
 
=== Circumscribe the Triangle with an Ellipse ===
 
Resume emacs and add the following lines so that the entire function appears as:
<syntaxhighlight lang="swift" highlight="13-14,16-17">
     override func setup(canvasSize:Size, canvas:Canvas) {
         let lineWidth = LineWidth(width:3)
         let lineWidth = LineWidth(width:3)
         canvas.paint(lineWidth)
         canvas.render(lineWidth)


         let blue = StrokeStyle(color:Color(red:100, green:100, blue:255))
         let blue = StrokeStyle(color:Color(red:100, green:100, blue:255))
         canvas.paint(blue)
         canvas.render(blue)


         let lines = Lines(from:Point(x:110, y:150), to:Point(x:200, y:150))
         let lines = Lines(from:Point(x:110, y:150), to:Point(x:200, y:150))
         lines.lineTo(Point(x:155, y:250))
         lines.lineTo(Point(x:155, y:250))
         lines.lineTo(Point(x:110, y:150))
         lines.lineTo(Point(x:110, y:150))
         canvas.paint(lines)
         canvas.render(lines)


         let violet = StrokeStyle(color:Color(.blueviolet))
         let violet = StrokeStyle(color:Color(.blueviolet))
         canvas.paint(violet)
         canvas.render(violet)


         let ellipse = Ellipse(center:Point(x:155, y:200), radiusX:120, radiusY:55)
         let ellipse = Ellipse(center:Point(x:155, y:200), radiusX:120, radiusY:55)
         canvas.paint(ellipse)
         canvas.render(ellipse)
     }</syntaxhighlight>
     }</syntaxhighlight>


{{Observe|: Section 3|
# What do you observe?
# Why do you think the class {{SwiftClass|Ellipse}} is used to draw a circle?
# What is the purpose of <syntaxhighlight lang="swift" inline>radiusX</syntaxhighlight> and <syntaxhighlight lang="swift" inline>radiusY</syntaxhighlight>?
}}
== Exercises ==
{{Exercises|
* {{Assignment|J1503}} Create a journal and answer all questions.  Be sure to include all sections of the journal, properly formatted.
* On a single canvas:
*# Use your pyramid (built of "bricks") from previous exercises.  Split your pyramid so that a few rows near the apex are separated from the body of the pyramid.
*# Alter the pyramid so that these separated rows (on top) are drawn using a triangle (rather than by rows of bricks).
*# Inside the triangle, draw an eye.
*# Draw the entire pyramid in shades of green.  (Use at least three different shades of green.)
* {{MMMAssignment|M1503-28}}
}}


= Exercises =
[[Category:IGIS]]
Continuing your previous project:
# Split your pyramid so that a few rows near the apex are separated from the body of the pyramid
# Alter the pyramid so that these separated rows (on top) are drawn using a triangle (rather than by a rows of bricks)
# Inside the triangle, draw an eye
# Draw the entire pyramid in shades of green.  (Use at least three different shades of green.)  See this [[W1502 Object Attributes#Research|note]] for more information about colors.

Latest revision as of 14:30, 17 February 2022

Within these castle walls be forged Mavens of Computer Science ...
— Merlin, The Coder
Dollar Bill

Prerequisites[edit]

Research[edit]

  • Examine the back of a dollar bill. Note the pyramid on the left-hand side.

Prepare[edit]

Create a new Scenes shell project within your Experiences directory:

ty-cam@codermerlin:~$  cd ~/Experiences

ty-cam@codermerlin:~/Experiences$  git clone https://github.com/TheCoderMerlin/ScenesShellBasic W1503


Enter the Sources/ScenesShell directory of the new project:

ty-cam@codermerlin:~/Experiences$  cd W1503/Sources/ScenesShell/


Start button green arrow
Run the program.

ty-cam@codermerlin:~/Experiences/W1503/Sources/ScenesShell$  run


Ensure that you are logged on to the wiki. Then, click on the Tools menu followed by right-clicking on IGIS and selecting the menu item Open in New Window or Open in New Tab.

You'll know you're successful if you see the title bar change to "Coder Merlin: IGIS". (The browser window will be blank because we haven't added any graphics yet.)

Hint.pngHelpful Hint
It's useful to bookmark this page in your browser.

Experiment[edit]

Stop button red ex
Stop the running program.

Return to the console and press CONTROL-C

Render a Line[edit]

Open the file Background.swift in emacs.

Add a new method (below init) as follows:

    override func setup(canvasSize:Size, canvas:Canvas) {
        let lineWidth = LineWidth(width:3)
        canvas.render(lineWidth)

        let blue = StrokeStyle(color:Color(red:100, green:100, blue:255))
        canvas.render(blue)

        let lines = Lines(from:Point(x:110, y:150), to:Point(x:200, y:150))
        canvas.render(lines)
     }

Remember to save the file, then suspend emacs.

Hint.pngHelpful Hint

An online tool to determine color values is available here. Use the RGB values in the following constructor to generate any custom color:

Color(red:UInt8, green:UInt8, blue:UInt8)


Start button green arrow
Run the program and refresh the browser page.


ObserveObserveIcon.png
Observe, Ponder, and Journal: : Section 1
  1. What do you observe?
  2. What is the purpose of LineWidth?
  3. Why do we draw lines with StrokeStyle rather than FillStyle?


Stop button red ex
Stop the running program.

Render a Triangle[edit]

Resume emacs and edit the setup function so it appears as follows:

    override func setup(canvasSize:Size, canvas:Canvas) {
        let lineWidth = LineWidth(width:3)
        canvas.render(lineWidth)

        let blue = StrokeStyle(color:Color(red:100, green:100, blue:255))
        canvas.render(blue)

        let lines = Lines(from:Point(x:110, y:150), to:Point(x:200, y:150))
        lines.lineTo(Point(x:155, y:250))
        lines.lineTo(Point(x:110, y:150))
        canvas.render(lines)
     }
Start button green arrow
Run the program and refresh the browser page.


ObserveObserveIcon.png
Observe, Ponder, and Journal: : Section 2
  1. What do you observe?
  2. Which statements draw the triangle?
  3. What would happen without line 10 (in the above listing)?


Stop button red ex
Stop the running program.

Circumscribe the Triangle with an Ellipse[edit]

Resume emacs and add the following lines so that the entire function appears as:

    override func setup(canvasSize:Size, canvas:Canvas) {
        let lineWidth = LineWidth(width:3)
        canvas.render(lineWidth)

        let blue = StrokeStyle(color:Color(red:100, green:100, blue:255))
        canvas.render(blue)

        let lines = Lines(from:Point(x:110, y:150), to:Point(x:200, y:150))
        lines.lineTo(Point(x:155, y:250))
        lines.lineTo(Point(x:110, y:150))
        canvas.render(lines)

        let violet = StrokeStyle(color:Color(.blueviolet))
        canvas.render(violet)

        let ellipse = Ellipse(center:Point(x:155, y:200), radiusX:120, radiusY:55)
        canvas.render(ellipse)
     }
ObserveObserveIcon.png
Observe, Ponder, and Journal: : Section 3
  1. What do you observe?
  2. Why do you think the class Ellipse is used to draw a circle?
  3. What is the purpose of radiusX and radiusY?

Exercises[edit]

ExercisesExercisesIcon.png
  •  J1503  Create a journal and answer all questions. Be sure to include all sections of the journal, properly formatted.
  • On a single canvas:
    1. Use your pyramid (built of "bricks") from previous exercises. Split your pyramid so that a few rows near the apex are separated from the body of the pyramid.
    2. Alter the pyramid so that these separated rows (on top) are drawn using a triangle (rather than by rows of bricks).
    3. Inside the triangle, draw an eye.
    4. Draw the entire pyramid in shades of green. (Use at least three different shades of green.)
  •  M1503-28  Complete  Merlin Mission Manager  Mission M1503-28.