Skip to content

Instantly share code, notes, and snippets.

@aguegu
Created September 30, 2013 07:24
Show Gist options
  • Save aguegu/6760382 to your computer and use it in GitHub Desktop.
Save aguegu/6760382 to your computer and use it in GitHub Desktop.
replace sin curve with sections of lines
package demo;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import processing.core.PApplet;
import processing.core.PVector;
import demo.DmPoint;
public class Demo extends PApplet {
private static final long serialVersionUID = 7426719630987497221L;
private DmPoint _locations[];
private ArrayList<DmPoint> _path;
public double getDistance(PVector x, PVector y, PVector z) {
PVector s1 = PVector.sub(y, x);
PVector s2 = PVector.sub(z, x);
return s1.cross(s2).mag() / s1.mag();
}
private void findMax(PVector p[], int length) {
PVector s1 = PVector.sub(p[length - 1], p[0]);
double max = 0;
int index = 0;
for (int i = 1; i < length - 1; i++) {
PVector s2 = PVector.sub(p[i], p[0]);
double distance = s1.cross(s2).mag() / s1.mag();
if (max < distance) {
max = distance;
index = i;
}
}
if (max > 0.1) {
_path.add((DmPoint) p[index]);
findMax(p, index);
findMax(Arrays.copyOfRange(p, index, length), length - index - 1);
}
}
private void output() {
for (DmPoint p : _path) {
print(p.getIndex());
print(p);
}
}
public void setup() {
this.size(720, 200);
this.scale(2);
noSmooth();
_locations = new DmPoint[360];
for (int i = 0; i < _locations.length; i++) {
_locations[i] = new DmPoint(this, i, i, 50 + sin(i * PI / 180) * 40);
_locations[i].draw();
}
_path = new ArrayList<DmPoint>();
_path.clear();
_path.add((DmPoint) _locations[0]);
stroke(255, 0, 0);
findMax(_locations, _locations.length);
Collections.sort(_path, new DmPoint.DmPointComparator());
_path.add((DmPoint) _locations[_locations.length - 1]);
output();
for (int i = 0; i < _path.size() - 1; i++) {
DmPoint.line(_path.get(i), _path.get(i + 1));
}
print("total ");
print(_path.size());
println(" points.");
}
public void draw() {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment