Sorting points in an arraylist clockwise

Hello,

I have an arraylist that contains a number of points that all belong to the edge of an open surface. I am trying to sort the list so that the points are in clockwise order. I have the center c and the normal n of the surface, but still I have no idea of how to do the sorting in terms of code. Any help would be much appreciated!

Answers

  • edited February 2016

    in theory make a class PointInCircle that holds the PVector and the angle

    put this class PointInCircle in an arraylist

    now calc each angle using atan2 or so

    then make compare method in the class iirc and sort it by angle

    gotoloop has written a lot about it

  • google the keywords here in the forum

  • I have the comparison of angles already. Any idea of how to do the sorting afterwards? I've seen examples in stack overflow but I don't understand the syntax

  • edited February 2016 Answer ✓

    Ask @gotoloop

    Or google "sort" here in the forum

    Or click on the tag sort

  • Ok, thanks a lot for the reply!

  • Have you created a class that holds the point and the angle about the centre as suggested by chrisir?

    The way to sort the points depends on whether you have created a class or not. If you have a class then you can use Java's built in sort routines if not you are going to have to code your own sorting algorithm.

  • this is for finding you angle (0 degrees is to the right (east))

    code by the forum

    // angle 
    
    void setup()
    {
      size(400, 400);   // always first line
      rectMode(CENTER);
      noStroke();
    }
    
    
    void draw()
    {
      background(255); // white
    
      // atan2 returns the angle (in radians) 
      // from a specified point to the coordinate origin 
      // as measured from the positive x-axis
      float ang = atan2(mouseY-height*0.5, mouseX-width*0.5);
    
    
      // ---------------------------------------------------
      // using angle2 here 
    
      float angle2=ang;  
      if (angle2 < 0) 
        angle2 += TWO_PI;
    
      // text 
      fill(0); // black 
      text(degrees(angle2), 19, 19);
    
      // -------------------------------------------------
      // using ang here again  
    
      // and the rotating bar
      translate(width*0.5, height*0.5);
      rotate(ang);
    
      fill(200); // gray
      rect(0, 0, width*0.5, height*0.25);
    
      fill(255, 0, 0); // red 
      rect(width*0.16, 0, width*0.18, height*0.01);
    }
    
  • Thanks everyone for the reply! I figured it out eventually :)

  • Can you post it

  • Ok, there you go. I did it in a different way eventually

    `
    boolean isNeighbour; Particle pt0 = (Particle) arrayOfSideParticles.get(0); pt0.isChecked = true; arrayOfSortedEdgePoints.add(pt0); Particle selected = null;

    for (int j=0; j<pt0.linkedParticles.size (); j++) {
      Particle ptB = (Particle) pt0.linkedParticles.get(j);
    
      if (ptB.isBorder == true) {
        selected = ptB;
        ptB.isChecked = true;
        arrayOfSortedEdgePoints.add(ptB);
        break;
      }
    }
    
    while (true) {
      for (int i=0; i<arrayOfSideParticles.size (); i++) {
        Particle pt = (Particle) arrayOfSideParticles.get(i);
        if (selected != null) {
          if (pt.isChecked == false && selected.linkedParticles.contains(pt)) {
            arrayOfSortedEdgePoints.add(pt);
            selected = pt;
            selected.isChecked = true;
          }
        }
      }
    
      boolean isAllChecked = true;
      for (int i=0; i<arrayOfSideParticles.size (); i++) {
        Particle pt = (Particle) arrayOfSideParticles.get(i);
        if (pt.isChecked == false) {
          isAllChecked = false;
        }
      }
      if (isAllChecked) break;
    }`
    
Sign In or Register to comment.