Loading...
Processing Forum
Recent Topics
All Forums
Move this topic
Forum :
Share your Work
Programming Questions
Core Library Questions
Contributed Library Questions
Android Processing
Processing with Other Languages
Integration and Hardware
Library and Tool Development
Events and Opportunities
General Discussion
Sub forum :
Move this topic
Cancel
Programming Questions
clankill3r
sort ArrayList with PVector on distance to a point
in
Programming Questions
•
1 year ago
is it possible to sort a arrayList that hold's PVectors on distance to a given point?
Or do i need to wrap the PVector in a class for that?
1
Replies(4)
dr.mo
Re: sort ArrayList with PVector on distance to a point
1 year ago
make a class that implements Comparable<T> and that contains a PVector or better yet that derives from PVector. Lets say you call it PVectorSortable then an 'ArrayList<PVectorsSortable> myArray' can be sorted using Collections.sort(myArray)
more here:
http://docs.oracle.com/javase/tutorial/collections/interfaces/order.html
Leave a comment on dr.mo's reply
PhiLho
Re: sort ArrayList with PVector on distance to a point
1 year ago
Or use a Comparator, with it there is no need for a wrapper.
Leave a comment on PhiLho's reply
PhiLho
Re: sort ArrayList with PVector on distance to a point
1 year ago
int SIZE = 20;
int centerX, centerY;
PVector centerDot;
ArrayList<PVector> dots = new ArrayList<PVector>();
void setup()
{
size(400, 400);
centerX = width / 2;
centerY = height / 2;
centerDot = new PVector(centerX, centerY);
noStroke();
smooth();
}
void mousePressed()
{
PVector dot = new PVector(mouseX, mouseY);
// Trick: store dist in z to avoid computing it too often
float d = dot.dist(centerDot);
dot.z = d;
dots.add(dot);
}
void keyPressed()
{
noLoop();
background(255);
drawCenter();
float max = dist(0, 0, centerX, centerY);
// Do the sort
Collections.sort(dots, new CompareToCenter());
int i = 0;
int nb = dots.size();
for (PVector v : dots)
{
// This doesn't prove anything!
// fill(map(v.z, 0, max, 0, 255));
fill(map(i, 0, nb, 50, 250));
drawDot(v);
fill(#00FF00);
text("" + i, v.x, v.y);
i++;
}
}
void draw()
{
background(255);
drawCenter();
for (PVector v : dots)
{
drawDot(v);
}
}
void drawDot(PVector dot)
{
ellipse(dot.x, dot.y, SIZE, SIZE);
}
void drawCenter()
{
fill(#FF0000);
ellipse(centerX, centerY, SIZE, SIZE);
fill(0);
}
class CompareToCenter implements Comparator<PVector>
{
//@Override
int compare(PVector v1, PVector v2)
{
return int(v1.z - v2.z);
}
}
Leave a comment on PhiLho's reply
clankill3r
Re: sort ArrayList with PVector on distance to a point
1 year ago
thanks phi.lho
That was helpfull, i ended up with this:
ArrayList<PVector> dots = new ArrayList<PVector>();
void setup() {
size(800, 800);
for (int i = 0; i < 70; i++) {
PVector v = new PVector(random(0, width), random(0, height));
dots.add(v);
}
}
void draw() {
background(255);
fill(255, 0, 0);
for (int i = 0; i < dots.size(); i++) {
PVector v = dots.get(i);
ellipse(v.x, v.y, 5, 5);
}
PVector mousePos = new PVector(mouseX, mouseY);
Collections.sort(dots, new DistanceComparator(mousePos));
PVector closestDot = dots.get(0);
fill(0, 255, 0);
ellipse(closestDot.x, closestDot.y, 5, 5);
}
import java.util.Comparator;
public class DistanceComparator implements Comparator<PVector> {
PVector compareToVector;
public DistanceComparator(PVector compareToVector) {
this.compareToVector = compareToVector;
}
public int compare(PVector v1, PVector v2) {
float d1 = v1.dist(compareToVector);
float d2 = v2.dist(compareToVector);
if (d1 < d2) {
return -1;
}
else if (d1 > d2) {
return 1;
}
else {
return 0;
}
}
}
Leave a comment on clankill3r's reply
Change topic type
Topic Type :
Discussions
Questions
No of days :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
Change topic type
Cancel
Link this topic
Provide the permalink of a topic that is related to this topic
Permalink
Save
Close
Reply to clankill3r's question
Top
Reply
{"z4085862":[25080000001407017,25080000001407025],"z5322143":[25080000001403355],"z4106163":[25080000001403348,25080000001402287]}
Statistics
4
Replies
982
Views
0
Followers
Tags
No tags available for this topic.
Cancel
Actions
Permalink
Related Posts
Reflecting a vector from a plane
Sort a complex ArrayList with a lot...
Trying to understand matrices and r...
Simple Code Error
wth with arraylist : CocurrentModif...