Slow Framerate with 800+ Voronoi Points
in
Contributed Library Questions
•
1 years ago
Requires Lee Byrons Voronoi Mesh library.
Problem:
- Slow framerate once 800+ points are added.
Looking for optimization / suggestions to improve performance. As I am hoping to get it running very smoothly at the 1000 - 2000 point mark.
- import megamu.mesh.*;
- Voronoi voronoi;
- float[][] points = new float[0][2];
- int totalPoints = 800;
- ArrayList<PVector> pointsList = new ArrayList<PVector>();
- int selectDistance = 10;
- boolean selected = false;
- int index = 0;
- void setup()
- {
- size(1800, 900);
- //ImportPoints();
- GenerateVoronoiPoints();
- }
- void draw()
- {
- background(255);
- DrawVoronoi();
- }
- void ImportPoints()
- {
- String[] strLines = loadStrings("VoronoiPoints.txt");
- for(int i=0;i<strLines.length;i++)
- {
- String[] strList = split(strLines[i], ',');
- float x = new Float(strList[0]);
- float y = new Float(strList[1]);
- points = (float[][])append(points, new float[] {x, y});
- }
- }
- void DrawVoronoi()
- {
- voronoi = new Voronoi(points);
- MPolygon[] regions = voronoi.getRegions();
- for(int r=0;r<regions.length;r++)
- {
- regions[r].draw(this);
- stroke(0);
- ellipse(points[r][0],points[r][1],5,5);
- }
- }
- void GenerateVoronoiPoints()
- {
- PVector pv1;
- float x;
- float y;
- for(int i=0;i<totalPoints;i++)
- {
- x = random(5,width);
- y = random(5,height);
- pv1 = new PVector(x,y);
- if(!pointsList.contains(pv1))
- {
- pointsList.add(pv1);
- }
- }
- for(int p=0;p<pointsList.size();p++)
- {
- x = pointsList.get(p).x;
- y = pointsList.get(p).y;
- points = (float[][])append(points, new float[] {x, y});
- }
- }
- void mousePressed()
- {
- if(mouseButton == RIGHT)
- {
- points = (float[][])append(points, new float[] {mouseX, mouseY});
- }
- voronoi = new Voronoi(points);
- }
- void mouseDragged()
- {
- for(int i =0;i<points.length;i++)
- {
- if(!selected)
- {
- if (mouseX > points[i][0] - selectDistance
- && mouseX < points[i][0] + selectDistance
- && mouseY > points[i][1] - selectDistance
- && mouseY < points[i][1] + selectDistance)
- {
- selected = true;
- index = i;
- }
- }
- }
- if(selected)
- {
- points[index][0] = mouseX;
- points[index][1] = mouseY;
- }
- }
- void mouseReleased()
- {
- selected = false;
- index = -1;
- }
So this is working, however once I hit 800+ points the framerate drops once you start dragging points around / adding points.
Can anyone help me with the framerate issues, possible suggestions for Optimization?
Thanks! This thing is kinda fun to play around with too.
Side note: This is part of a larger project, cut out in its simplest form to test for efficiency.
1