sudha
    • Create new note
    • Create a note from template
      • 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
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee
    • 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
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control 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
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee
  • 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
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # Joy Live Coding ## Examples #### [Sierpinski Triangle](https://en.wikipedia.org/wiki/Sierpi%C5%84ski_triangle) ![image](https://hackmd.io/_uploads/rJSYrs1dJl.png) #### Staircase ![image](https://hackmd.io/_uploads/BkJi-hk_1e.png) #### [Tessellation](https://en.wikipedia.org/wiki/Tessellation) This will give you a random pattern each time. ![image](https://hackmd.io/_uploads/ryBX1nkdyg.png) #### Pointy flower This one is made entirely with rectangles. ![image](https://hackmd.io/_uploads/BkXUl31ukx.png) ## Live Coding Environment (0, 0) is the center of the canvas. The default canvas size is 500, 500. ### Links: - [kaustubh.page/joy](https://kaustubh.page/joy/) - Backup: [ocaml-joy.netlify.app](https://ocaml-joy.netlify.app/) ## Basic Shapes ```ocaml let s1 = circle 50;; show [s1];; let s2 = rectangle 100 100;; show [s2];; ``` ![image](https://hackmd.io/_uploads/Bk7GQj1dJe.png) > ### Clearing the canvas > > To reset the canvas to blank again, we call the `clear ()` function. > > ```ocaml > clear ();; > ``` ## Transformations ### Translate ```ocaml let c1 = circle 50;; let c2 = circle 50 |> translate 100 0;; let c3 = circle 50 |> translate (-100) 0;; show [c1; c2; c3];; ``` ![image](https://hackmd.io/_uploads/SJ6wz21dkg.png) #### Exercise ##### Four circles pattern ![Screenshot from 2024-04-05 15-08-39](https://hackmd.io/_uploads/SJl6Urp1R.png) ##### Olympic Rings > Hints: > - use radius = 50 for all circles > - left-most circle is centered at (-110, 0) > - bottom-left circle is centered at (-55, 0) ![image](https://hackmd.io/_uploads/S1eJ83ydke.png) ### Rotate ```ocaml let s1 = rectangle 100 100;; let s2 = rectangle 100 100 |> rotate 45;; show [s1; s2];; ``` ![image](https://hackmd.io/_uploads/ryYyDnk_ye.png) ### Scale ```ocaml let s1 = rectangle 100 100;; let s2 = rectangle 100 100 |> scale 2.0;; let s3 = rectangle 100 100 |> scale 0.5;; show [s1; s2; s3];; ``` ![image](https://hackmd.io/_uploads/SkTGvh1d1e.png) ### Combining Transformations ```ocaml let r1 = rectangle 200 200;; let r2 = r1 |> rotate 45 |> scale (1.0 /. Float.sqrt(2.0));; show [r1; r2];; ``` ![image](https://hackmd.io/_uploads/ryfsp3Jdyx.png) ## Combining shapes With `complex` type ```ocaml let c1 = circle 50;; let c2 = circle 100;; let donut = complex [c1; c2];; show [donut];; ``` ![image](https://hackmd.io/_uploads/HyEna2Ju1x.png) ### Exercise * Create a 4 donuts pattern ## Higher Order Transformations ```ocaml let c1 = circle ~c:{x = (-100.); y = 50.} 50;; let row = c1 |> repeat 10 (translate 20 0);; show [row];; ``` ![image](https://hackmd.io/_uploads/SkG2apxdye.png) ```ocaml let l1 = line {x = 100. ; y = 0.};; let spokes = l1 |> repeat 20 (rotate 18);; show [spokes];; ``` ![image](https://hackmd.io/_uploads/HyZRTpedJl.png) ```ocaml let r1 = rectangle 100 100;; let spin1 = r1 |> repeat 18 (rotate 10);; show [ spin1 ];; ``` ![image](https://hackmd.io/_uploads/HyrgCTgdJg.png) ```ocaml let r1 = rectangle 300 300;; let spin2 = r1 |> repeat 72 (compose (rotate 5) (scale 0.92));; show [ spin2 ];; ``` ![image](https://hackmd.io/_uploads/rJYzApl_ke.png) **Colours** ```ocaml let c1 = circle 50 |> with_stroke blue;; show [c1];; let c1 = circle 50 |> with_fill red;; ``` Example with colours: ```ocaml let pink = rgb 255 182 193;; let r1 = rectangle 200 200 |> with_fill pink |> with_stroke pink;; let r1_s = r1 |> repeat 10 (rotate 18);; let tomato = rgb 255 99 71;; let r2 = rectangle 175 175 |> with_fill tomato |> with_stroke tomato;; let r2_s = r2 |> rotate 9 |> repeat 10 (rotate 18);; let r_blue = rgb 65 105 225;; let r3 = rectangle 153 153 |> with_fill r_blue |> with_stroke r_blue;; let r3_s = r3 |> rotate 18 |> repeat 10 (rotate 18);; show [r1_s; r2_s; r3_s];; ``` **Spiral** ```ocaml let c1 = circle ~c:{x = 100.; y = 0.} 50;; let shape = c1 |> repeat (36*4) (compose (rotate 10) (scale 0.97));; ``` **Sierpinski Triangle** ```ocaml let mid (p1: float point) (p2: float point) = let find_nearest n = Float.round n |> int_of_float in point (find_nearest ((p1.x +. p2.x) /. 2.)) (find_nearest ((p1.y +. p2.y) /. 2.)) let rec sierpinski degree points = let t = polygon points in show [t]; let p = Array.of_list points in if degree > 0 then begin sierpinski (degree - 1) [p.(0); mid p.(0) p.(1); mid p.(0) p.(2)]; sierpinski (degree - 1) [p.(1); mid p.(0) p.(1); mid p.(1) p.(2)]; sierpinski (degree - 1) [p.(2); mid p.(2) p.(1); mid p.(0) p.(2)] end else () ``` ## Lambda Functions ```ocaml let id = fun x -> x;; id 10;; (* int : 10 *) let square = fun x -> x * x;; square 5;; (* int : 25 *) ``` They can be used without naming them, like: ```ocaml List.init 10 (fun i -> i + 1);; (* [1; 2; 3; 4; 5; 6; 7; 8; 9; 10] *) ``` ## map-filter **map** ```ocaml let multiples_of_fifty = List.init 9 (fun i -> i) (* [0; 1; 2; 3; 4; 5] *) |> List.map (fun x -> x * 50);; (* [0; 50; 100; 150; 200] *) let concentric_circles = multiples_of_fifty |> List.map (fun r -> circle r);; show concentric_circles;; ``` **filter** ```ocaml let multiples_of_hundred = multiples_of_fifty |> List.filter (fun x -> x mod 100 = 0);; ``` ## Tessellation ```ocaml let tessellate tile_size tile_fn = let n_tiles = 500 / tile_size in let tile_indices = List.init n_tiles (fun i -> i) in let xys = tile_indices |> List.map (fun x -> List.map (fun y -> (x, y)) tile_indices) |> List.flatten in let shrink_factor = (float_of_int tile_size) /. 500.0 in let move_factor_fn = fun x_or_y -> x_or_y * tile_size + tile_size / 2 - 250 in let tessellation = xys |> List.map (fun (x, y) -> tile_fn x y |> scale shrink_factor |> translate (move_factor_fn x) (move_factor_fn y)) |> complex in show [ tessellation ];; ``` ## Related explorations - `fold` operation https://cs3110.github.io/textbook/chapters/hop/fold.html

    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