Code Monkey home page Code Monkey logo

curvy's Introduction

curvy

What is an L-System?

Check out the wikipedia article here. All four of the curves that I implemented are used as examples in the article.

Overview

To draw a curve, these are the necessary inputs:

  • state: Tracks the current state of the curve.
  • constants: Interpreted as instructions to perform an action such as drawing or turning.
  • variables: Characters to be replaced based on the rules of production
  • rules of production: Describes how to replace variables to systematically create new iterations of the curve.

Hilbert Curve

THe Hilbert Curve is a great example:

variables : A, B
constants : F + −
(F == draw, + == turn right 90°, - == turn left by 90°)
state : A
production rules :
A → − B F + A F A + F B −
B → + A F − B F B − F A +

Since the initial state is A and the rules dictate that A → − B F + A F A + F B −, this is what state looks like after the first iteration:

B F + A F A + F B −

hilbert 1

The second iteration will iterate through the previous state and replace all variables again:

+-AF+BFB+FA-F-+BF-AFA-FB+F+BF-AFA-FB+-F-AF+BFB+FA-+

hilbert 2

##Implementation

To replace variables, I used regex:

setInstructions(parser){
  this.state = this.state.replace(new RegExp(parser, 'gi'), match => {
    return this.rules[match];
  });
}

Once the desired state is reached, I match all the constants and follow the instructions appropriately:

const matched = this.state.match(new RegExp(parser + "|\\+|-", 'gi'));

No vectors were used in this implementation. Points are calculated using trigonometry and then connected to make a line:

this.x += Math.round((Math.cos(this.currentAngle) * canvas.height) / offset);
this.y += Math.round((Math.sin(this.currentAngle) * canvas.height) / offset);

Finally, setTimeout is used to asynchronously draw using Canvas:

if(index < array.length - 1 ){
  let timeOutkey = setTimeout(() => this.async(array, index + 1), delay);
  this.timeoutKeys.push(timeOutkey);
}

curvy's People

Watchers

Martin Camacho 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.