Skip to content

Instantly share code, notes, and snippets.

@gunungloli666
Created February 8, 2014 15:45
Show Gist options
  • Save gunungloli666/8885682 to your computer and use it in GitHub Desktop.
Save gunungloli666/8885682 to your computer and use it in GitHub Desktop.
package fjr.hull.test;
import java.awt.Point;
import java.util.ArrayList;
import signalprocesser.voronoi.VPoint;
import signalprocesser.voronoi.VoronoiAlgorithm;
import signalprocesser.voronoi.representation.triangulation.TriangulationRepresentation;
import signalprocesser.voronoi.representation.triangulation.VHalfEdge;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class ConcaveHull extends Application{
int width_ = 400;
int height_ = 400;
Canvas canvas;
GraphicsContext gc;
@Override
public void start(Stage stage) throws Exception {
Group root = new Group();
canvas = new Canvas(width_ - 30, height_ - 30);
gc = canvas.getGraphicsContext2D();
canvas.setTranslateX(15);
canvas.setTranslateY(15);
listPoint = new ArrayList<VPoint>();
root.getChildren().add(canvas);
tr = new TriangulationRepresentation();
tr.setCalcCutOff(new TriangulationRepresentation.CalcCutOff() {
@Override
public int calculateCutOff(TriangulationRepresentation representation) {
return 120; // kayaknya ini nilai ideal...
}
});
stage.setScene(new Scene(root, width_, height_));
stage.show();
stage.setTitle("concave-hull: klik di atas canvas");
canvas.addEventHandler(MouseEvent.MOUSE_PRESSED,
new EventHandler<MouseEvent>() {
public void handle(MouseEvent t) {
int x = (int) t.getX();
int y = (int) t.getY();
listPoint.add(tr.createPoint(x,y));
draw(gc, null, null, null, null);
concaveHull();
}
});
Button btn = new Button(){{
setText("CLEAR");
setTranslateX(10);
setTranslateY(10);
setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent arg0) {
listPoint.clear();
draw(gc, null, null, null, null);
concaveHull();
}
});
}};
root.getChildren().add(btn);
}
ArrayList<VPoint> listPoint;
public void createRandomDot() {
int width = ((int) canvas.getWidth()) - 35;
int height = ((int) canvas.getHeight()) - 35;
for (int i = 0; i < 1; i++) {
int c = ((int) (Math.random() * (width)));
int d = ((int) (Math.random() * (height)));
if (c + 35 < width)
c += 35;
if (d + 35 < height)
d += 35;
listPoint.add(tr.createPoint(c,d));
}
}
TriangulationRepresentation tr;
public void concaveHull(){
if( gc == null )
return;
gc.setStroke(Color.BLACK);
VoronoiAlgorithm.generateVoronoi(tr , listPoint);
VHalfEdge outeredge = tr.getOuterEdge();
if ( outeredge==null || outeredge.next==null ) {
return;
}
VHalfEdge curredge = outeredge;
do {
gc.strokeLine( curredge.getX() , curredge.getY() , curredge.next.getX() , curredge.next.getY() );
} while ( (curredge=curredge.next).next!=null && curredge!=outeredge );
}
public void draw(GraphicsContext gc, ArrayList<Point> list, Color color,
ArrayList<Point> secondList, Color c) {
gc.setFill(Color.WHITE);
gc.fillRect(0, 0, canvas.getWidth(), canvas.getHeight());
gc.setFill(Color.RED);
for (int i = 0; i < listPoint.size(); i++) {
gc.fillOval(listPoint.get(i).x - 2.5, listPoint.get(i).y - 2.5, 5,
5);
}
}
public static void main(String[] args){
launch(args);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment