Coder Social home page Coder Social logo

micycle1 / spliner Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 1.0 367 KB

Piecewise curve approximation for Processing

License: BSD 3-Clause "New" or "Revised" License

Java 100.00%
bezier-curves piecewise-approximation curve-fitting processing processing4 algorithms shape-fitting

spliner's Introduction

Spliner

Piecewise curve approximation for Processing 4+.

Spliner constructs piecewise curves that have the best fit to a list of points. Four algorithms are implemented:

  • Quad + Bezier curve fitting
  • Smooth Fitting
  • Least-square fitting
  • Polygon fitting

Algorithms by Andrés Solís Montero.

Usage

List<PVector> points;
Fitting fitting = new BezierFitting(); // or new LeastSquareFitting(), etc.
List<Shape> beziers = fitting.fitCurve(points);

for (var curve : curves) {
    if (curve instanceof CubicCurve) {
        var c = (CubicCurve) curve;
        bezier(c.getX1(), c.getY1(), c.getCtrlX1(), c.getCtrlY1(), c.getCtrlX2(), c.getCtrlY2(), c.getX2(), c.getY2());
    } else { // straight-line segments
        var l = (Segment) curve;
        line(l.getX1(), l.getY1(), l.getX2(), l.getY2());
    }
}

Example

Processing Code...
import processing.javafx.*;
import micycle.spliner.fitting.*;
import micycle.spliner.geom.*;
import java.util.ArrayList;
import java.util.List;

List<PVector> points;
List<Line> curves;
int fittingType = 0;

void setup() {
  size(800, 800, FX2D);
  colorMode(HSB, 1, 1, 1);

  populatePoints();
  fit();
}

void draw() {
  background(0, 0, 1);

  int i = 0;
  for (var curve : curves) {
    stroke(color(i * (1f / curves.size()), 1, 1));
    strokeWeight(10);
    if (curve instanceof CubicCurve) {
      var c = (CubicCurve) curve;
      bezier(c.getX1(), c.getY1(), c.getCtrlX1(), c.getCtrlY1(), c.getCtrlX2(), c.getCtrlY2(), c.getX2(), c.getY2());
      stroke(color(0.4, 1, 0.8));
      strokeWeight(2);
      line(c.getX1(), c.getY1(), c.getCtrlX1(), c.getCtrlY1()); // cp line
      line(c.getX2(), c.getY2(), c.getCtrlX2(), c.getCtrlY2()); // cp line
    } else { // straight-line segments
      var l = (Segment) curve;
      line(l.getX1(), l.getY1(), l.getX2(), l.getY2());
    }
    i++;
  }
}

void fit() {
  Fitting f = null;
  switch (fittingType % 4) {
  case 1 :
    f = new BezierFitting();
    break;
  case 0 :
    f = new LeastSquareFitting();
    break;
  case 2 :
    f = new PolygonFitting();
    break;
  case 3 :
    f = new SmoothFitting();
    break;
  }
  curves = f.fitCurve(points);
}

void populatePoints() {
  points = new ArrayList<>();
  for (int x = 0; x < width; x+=20) {
    points.add(new PVector(x, noise(x+frameCount) * height));
  }
}

void mousePressed() { // click to fit a new point set
  populatePoints();
  fit();
}

void keyPressed() { // press to switch fitting algorithm
  fittingType++;
  fit();
}

spliner's People

Contributors

micycle1 avatar

Stargazers

 avatar  avatar

Watchers

 avatar  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.