Git Product home page Git Product logo

chaos-game's Introduction

Chaos Game

One day on youtube there was a video which name contains Chaos Game, and my love of superior math was amazed by the video.

Processes mentioned on video:

  1. Draw a shape.
  2. Pick a random point from the inside of the shape
  3. Pick a random corner and put a dot at the midpoint of these two points.
  4. Do the 3rd process for the new dot.

Writing the Script

When writing these operations to the code, we need to define certain algorithms.

  • How do we understand the random point was in the shape?
  • When our shape has 4 or more corners, we need to dismiss some corners.* How?

First of all, I use the turtle module for the graphical interface.
It's really simple and I can draw shapes by just use some coordinates.

To pick a random point from inside of the shape, I use that algorithm:

Areas

If we draw triangles with a random point and 2 corners, the area of triangles must be equal to the area of the shape.
Nor the point isn't inside of the shape.

1+2+3+4 == Area of Squere

Then we chose a random corner and find the midpoint between the corner and the point.
That is the easiest part of the code. Because has a simple math formula.
And when we put dots to midpoints, the script must be finished.

After a while of this progression, I got shapes like these.

Images

4+ Corner Problem

That algorithm is perfectly run at the triangle, but if the shape has 4 or more corners we'll see a miscorrect pattern.

unchanged algorithm with sqruare

You can change the pattern by changing the corner order.
If we chose a different corner from the last process there won't appear a misspattern. (Example 2nd )
Or you can dismiss the next corner (Example 3rd) or the next 2nd corner (Example 4th).

chaos-game's People

Contributors

mkbv avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.