111-1 Note (4-1)
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Write
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Help
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Write
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # CG HW1 Report ## Line algorithm implementation We first calculate the vector $(dx, dy)$ and the distance $D$ from $(x_1, y_1)$ to $(x2, y2)$. \begin{cases} dx = x_2-x_1 \\ dy = y_2-y_1 \end{cases} $$ D = \sqrt{dx^2+dy^2} $$ Then, with the vector and distance calculated, we can simply plot the graph by interpolation. That is, for each each "step" $d$ we make, we calculate the "completion ratio" until $d=D$, plot the line with the following formula: $$ (x, y) = (x_1 + \frac{d}{D}dx,\ y_1 + \frac{d}{D}dy) \quad where \quad d \in [0,D] $$ > ![image](https://hackmd.io/_uploads/HJiaY6og1l.png) ## Circle algorithm implementation With the same idea as above, we can first evaluate the perimeter $P$ of a circle, $$ P = 2 \pi r $$ With the perimeter calculated, we need to transform "completion rate" to radian form, in order to apply trigonometric operations, $$ c = 2\pi \frac{d}{P} $$ We can then draw a circle with the classic circle formulae, $$ (x, y) = (x + r \cos{c},\ y+r \sin{c} ) \quad where \quad c \in [0, 2\pi] $$ > ![image](https://hackmd.io/_uploads/B1sbqTjeyg.png) ## Ellipse algorithm implementation Again, same energy, we need to find the ellipse's perimeter. However, currently there is no accurate formula for Ellipse Perimeter in math. Luckily, there is still a proximated alternative version that can be easily evaluated, by using $$ P = 2\pi\sqrt{\frac{{2r_1}^2 + {2r_2}^2}{2}} $$ we get perimeter which gives the result at most 5% more than the real perimeter, which is acceptable as it may cause at most 5% curve segment be drawn twice. Cool. Then we can retrieve the completion rate with the same operation $$ c = 2\pi \frac{d}{P} $$ Draw a ellipse with the classic ellipse formulae, $$ (x, y) = (x + r_1 \cos{c},\ y+r_2 \sin{c} ) \quad where \quad c \in [0, 2\pi] $$ > ![image](https://hackmd.io/_uploads/H12NqaigJl.png) ## Bezier curve algorithm implementation Finally, here comes the boss attack. Bezier curve has no easy way to evaluate its length $L$. My way to implement it is to abandon the accuate perimeter evaluation, using a value greater than the actual length $L_e \ge L$. I chose $$ L_{e} = |\overrightarrow{p_1p_2}| + |\overrightarrow{p_2p_3}| + |\overrightarrow{p_3p_4}| $$ Completion rate is the same as a simple line (which we omitted in the previous line example) $$ c = \frac{d}{L_e} $$ Draw the line with the bezier curve algorithm $$ B(c) = p_1(1-c)^3 + 3 p_2 c(1-c)^2 + 3p_3 c^2(1-t) + p4 c^3 $$ > ![image](https://hackmd.io/_uploads/ry1RoTslke.png) :::info We may also set $L_e$ as a constant value, using linear interpolation to fill the gap > ![](https://i.imgur.com/8yAhh6A.png) > $L_e$ = 10 ::: ## Eraser implementation We draw the pixel with the same color as the background, within the range of $(x_1, y_1)$ and $(x_2, y_2)$. I added a check to ensure $x_1, y_1$ are always less than $x_2, y_2$. > ![image](https://hackmd.io/_uploads/ry-qsaslJl.png) ## :rainbow: Extra feature :rainbow: Click the rainbow button to enable <b><span style="color: #ff0000">R</span><span style="color: #ff7f00">A</span><span style="color: #eeee00">I</span><span style="color: #80ff00">N</span><span style="color: #00ff00">B</span><span style="color: #00ffff">O</span><span style="color: #0000ff">W</span></b> MODE!!! <!-- ![](https://i.imgur.com/wYNJvkZ.gif) --> > ![](https://i.imgur.com/Arksshy.gif) **Implementation** In `HW1.pde` where UI button is defined, we copied `shapeButton`'s code, modify it to fit our need, create a rainbow button. ```java=142 rainbowButton = new Button(width-90,10,30,30); rainbowButton.setBoxAndClickColor(color(250), color(150)); rainbowButton.setImage(loadImage("rainbow.png")); ``` In `Renderer.pde`, we defined two variables `UseRainbow` and `ColorShiftSpeed`. The former tells the renderer uses the rainbow utility or not, the latter set the rainbow rotation speed. Also, there is a private field `ColorShift` records current rotation. ```java=1 public boolean UseRainbow = false; public float ColorShiftSpeed = 148.763; float ColorShift = 0; ``` foreach draw call, `ColorShift` value grows, until it reach 1, at that time, it will be reset to 0. We will draw rainbow color base on this value. ```java=17 public void run(){ //... ColorShift += ColorShiftSpeed / 10000; if(ColorShift > 1) ColorShift %= 1; } ``` We also create a function to implement rainbow toggle ```java=45 public void toggleRainbow() { UseRainbow = !UseRainbow; } ``` We can now reference in `HW1.pde` ```java=36 rainbowButton.run(()->{shapeRenderer.toggleRainbow();}); ``` Here comes the most important part, how to draw in rainbow color? Below is the RGB value of rainbow color ![](https://i.imgur.com/hR2ETLp.gif) Observe that RGB value can be splitted into 5 phases, - **Phase 1**: (255,**0**,0) → (255,**255**,0) - **Phase 2**: (**255**,255,0) → (**0**,255,0) - **Phase 3**: (0,**255**,0) → (0,**0**,255) - **Phase 4**: (**0**,0,255) → (**255**,0,255) - **Phase 5**: (255,0,**255**) → (255,0,**0**) by using these relationships, we can write the below code in `util.pde` ```java=153 color evalColor(float t) { // Use Rainbow? if(!UseRainbow) return _color; // Apply animated color t += ColorShift; if(t > 1) t -= 1; // scalar [0-1] to rgb int phase = (int)(t * 6); // 0~6 t = (t % (1/6.0)) * 6; // normalize t to 0~1 scalar within that phase switch(phase) { case 0: return color(255, 255*t, 0); case 1: return color(255-255*t, 255, 0); case 2: return color(0, 255, 255*t); case 3: return color(0, 255-255*t, 255); case 4: return color(255*t, 0, 255); case 5: return color(255, 0, 255-255*t); case 6: return color(255, 0, 0); } return -1; } ``` Since all of our implementation has the parameter `complete_ratio` ($c$), we can easily change all color calls to this function to set rainbow color! <!-- \begin{cases} \text{open,} &\quad\text{if RMSD}_\text{s-open}\ge6, \text{RMSD}_\text{closed}\ge6\\ \text{closed,} &\quad\text{if RMSD}_\text{closed}\le2 \\ \text{semiopen,} &\quad\text{if RMSD}_\text{s-open}\le2\\ \text{transition,} &\quad\text{otherwise.} \\ \end{cases} -->

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully