<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
      <title>Tagged with compare() - Processing 2.x and 3.x Forum</title>
      <link>https://forum.processing.org/two/discussions/tagged/feed.rss?Tag=compare%28%29</link>
      <pubDate>Sun, 08 Aug 2021 19:37:20 +0000</pubDate>
         <description>Tagged with compare() - Processing 2.x and 3.x Forum</description>
   <language>en-CA</language>
   <atom:link href="/two/discussions/taggedcompare%28%29/feed.rss" rel="self" type="application/rss+xml" />
   <item>
      <title>Null Pointer Exception in Eclipse using JUnit and Comparator</title>
      <link>https://forum.processing.org/two/discussion/26660/null-pointer-exception-in-eclipse-using-junit-and-comparator</link>
      <pubDate>Mon, 05 Mar 2018 21:56:57 +0000</pubDate>
      <dc:creator>httpwilfred</dc:creator>
      <guid isPermaLink="false">26660@/two/discussions</guid>
      <description><![CDATA[<p>I've been working on a processing sketch for pixel sorting in the Eclipse IDE. It's getting large and I'm not sure if everything is working correctly. I was hoping to start using JUnit test on small pieces. However, I can't get the the first test to run. I'm hoping someone on here has done something similar.</p>

<p>Here is the code the JUnit test:</p>

<pre><code>package graham.wilfred.pixelsort;

import static org.junit.Assert.assertEquals;
import java.util.Comparator;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import processing.core.PApplet;

public class AlphaComparatorTest extends PApplet {

    @ BeforeAll
    public static void initAll() {
        PApplet.main("graham.wilfred.pixelsort.AlphaComparatorTest");
    }

    @ Test
    @ DisplayName("AlphaComparator -- Same Alpha Value Test")
    void sameAlphaValue() {
        Comparator&lt;Integer&gt; c = new AlphaComparator(this);
        int testColor1, testColor2;
        for (int i = 0; i &lt;= 255; i++) {
            testColor1 = color(i, i);
            testColor2 = color(255 - i, i);
            println("testColor1 in sameAlphaValue: " + testColor1);
            println("testColor2 in sameAlphaValue: " + testColor2);
    E&gt;      int compareValue = c.compare(testColor1, testColor2);
            assertEquals(compareValue, 0);
        }
    }

}
</code></pre>

<p>Here is the code for the AlphaComparator Class:</p>

<pre><code>package graham.wilfred.pixelsort;

import java.util.Comparator;
import processing.core.PApplet;

public class AlphaComparator implements Comparator&lt;Integer&gt; {

    PApplet parent;

    public AlphaComparator(PApplet p) {
        parent = p;
    }

    public int compare(Integer o1, Integer o2) {
        System.out.println("Compare Integer input: " + o1);
        System.out.println("Compare Integer input: " + o2);
        System.out.println("parent PApplet in AlphaComparator Class: " + parent);
        System.out.println(parent.color(0));
    E&gt;  Float b1 = parent.alpha(o1);
        Float b2 = parent.alpha(o2);
        return b1.compareTo(b2);
    }

}
</code></pre>

<p>The console output is:</p>

<pre><code>testColor1 in sameAlphaValue: 0
testColor2 in sameAlphaValue: 16777215
Compare Integer input: 0
Compare Integer input: 16777215
parent PApplet in AlphaComparator Class: graham.wilfred.pixelsort.AlphaComparatorTest@5a1c0542
-16777216
</code></pre>

<p>When I run in debug mode it gets into the compare method and I can see that parent contains all the normal stuff you would have in PApplet class.</p>

<p>The error is a Null Pointer Exception and it happens on E&gt; line in the AlphaComparator Class. I've narrowed it down to the parent.alpha() call. As you can see the parent.color() works. And when I run a normal processing sketch with an image it will literally call the compare method millions of times without a problem. So I am stumped. I suppose it could be Integer vs int but I tried using .intValue() and that didn't change anything.</p>

<p>Any advice would be appreciated.</p>
]]></description>
   </item>
   <item>
      <title>Sort a PVector Array</title>
      <link>https://forum.processing.org/two/discussion/19469/sort-a-pvector-array</link>
      <pubDate>Fri, 02 Dec 2016 14:26:08 +0000</pubDate>
      <dc:creator>digitalmartyn</dc:creator>
      <guid isPermaLink="false">19469@/two/discussions</guid>
      <description><![CDATA[<p>i'm using the Delauney Filter code and want to extract the cordinates from the array in order from low to high.</p>

<p>Sort(); isn't working for me. Presumably a basic syntax error. But i can't see it, and i've searched here for similiar answers but to no success. Help welcomed:</p>

<pre><code>import java.util.List;
import java.util.LinkedList;

int W = 800, H = 800;
int[] colors;
ArrayList&lt;Triangle&gt; triangles;

PFont myFont;


void setup() 
{
  size(800, 800);
  smooth();

  myFont = createFont("Arial", 10);
  textFont(myFont);
  textAlign(CENTER, CENTER);
  noFill();



  //Portrait of Jean-Charles de Cordes, by Rubens
  PImage buffer = loadImage("r.jpg");

  //Extract significant points of the picture
  ArrayList&lt;PVector&gt; vertices = new ArrayList&lt;PVector&gt;();

  //last number sets resolution - higher = fewer triangles
  EdgeDetector.extractPoints(vertices, buffer, EdgeDetector.SOBEL, 300, 100);

  //Add some points in the border of the canvas to complete all space
  for (float i = 0, h = 0, v = 0; i&lt;=1; i+=.05, h = W*i, v = H*i) {
    vertices.add(new PVector(h, 0));
    vertices.add(new PVector(h, H));
    vertices.add(new PVector(0, v));
    vertices.add(new PVector(W, v));
  }

  //Get the triangles using qhull algorithm. 
  //The algorithm is a custom refactoring of Triangulate library by Florian Jennet (a port of Paul Bourke... not surprisingly... :D) 
  triangles = new ArrayList&lt;Triangle&gt;();
  new Triangulator().triangulate(vertices, triangles);

  //Prune triangles with vertices outside of the canvas.
  Triangle t = new Triangle();
  for (int i=0; i &lt; triangles.size(); i++) {
    t = triangles.get(i); 
    if (vertexOutside(t.p1) || vertexOutside(t.p2) || vertexOutside(t.p3)) triangles.remove(i);
  }

  //Get colors from the triangle centers
  int tSize = triangles.size();
  colors = new int[tSize*3];
  PVector c = new PVector();
  for (int i = 0; i &lt; tSize; i++) {
    c = triangles.get(i).center();
    colors[i] = buffer.get(int(c.x), int(c.y));
  }

  //And display the result
  displayMesh();
}

//Util function to prune triangles with vertices out of bounds  
boolean vertexOutside(PVector v) { 
  return v.x &lt; 0 || v.x &gt; width || v.y &lt; 0 || v.y &gt; height;
}  

/**
  An algorithm that uses a custom implementation of a Sobel/Scharr operator to get the significant points of a picture.
*/

static class EdgeDetector
{
    static final int [][][] OPERATOR  = new int[][][]  {
        { {2,  2, 0}, { 2, 0,  -2}, {0,  -2, -2}},  //Sobel kernel
        { {6, 10, 0}, {10, 0, -10}, {0, -10, -6}}   //Scharr kernel
    };
    static final int SOBEL = 0, SCHARR = 1;  //Indexes of the kernels in the previous array

    //This method add significant points of the given picture to a given list
    static void extractPoints(List &lt;PVector&gt; vertices, PImage img, int op, int treshold, int res) 
    {     
          int col = 0, colSum = 0, W = img.width-1, H = img.height-1;

          //For any pixel in the image excepting borders            
          for (int Y = 1; Y &lt; H; Y += res) for (int X = 1; X &lt; W; X += res, colSum = 0) 
          {
              //Convolute surrounding pixels with desired operator       
              for (int y = -1; y &lt;= 1; y++) for (int x = -1; x &lt;= 1; x++, col = img.get((X+x), (Y+y))) 
                  colSum += OPERATOR[op][x+1][y+1] * ((col&gt;&gt;16 &amp; 0xFF)+(col&gt;&gt;8 &amp; 0xFF)+(col &amp; 0xFF));               
              //And if the resulting sum is over the treshold add pixel position to the list
              if (abs(colSum) &gt; treshold) vertices.add(new PVector(X, Y));                                  
          }  
     }    
}    

/** 
  A custom refactoring of Triangulate library by Florian Jennet
  Only minor changes to adapt it to my coding tastes. Not much interesting for anyone else, I think. : )
*/ 

import java.util.Comparator;
import java.util.Collections;
import java.util.Arrays;
import java.util.HashSet;

/**
    CircumCircle
    Calculates if a point (xp,yp) is inside the circumcircle made up of the points (x1,y1), (x2,y2), (x3,y3)
    The circumcircle centre is returned in (xc,yc) and the radius r. A point on the edge is inside the circumcircle
*/

public static class CircumCircle 
{ 
    public static boolean circumCircle(PVector p, Triangle t, PVector circle) 
    {
        float m1, m2, mx1, mx2, my1, my2;
        float dx, dy, rsqr, drsqr;

        /** Check for coincident points */
        if (abs(t.p1.y-t.p2.y) &lt; EPSILON &amp;&amp; abs(t.p2.y-t.p3.y) &lt; EPSILON) {
          //println("CircumCircle: Points are coincident.");
          return false;
        }

        if (abs(t.p2.y-t.p1.y) &lt; EPSILON) {
          m2 = - (t.p3.x-t.p2.x) / (t.p3.y-t.p2.y);
          mx2 = (t.p2.x + t.p3.x) * .5;
          my2 = (t.p2.y + t.p3.y) * .5;
          circle.x = (t.p2.x + t.p1.x) * .5;
          circle.y = m2 * (circle.x - mx2) + my2;
        }
        else if (abs(t.p3.y-t.p2.y) &lt; EPSILON) {
          m1 = - (t.p2.x-t.p1.x) / (t.p2.y-t.p1.y);
          mx1 = (t.p1.x + t.p2.x) * .5;
          my1 = (t.p1.y + t.p2.y) * .5;
          circle.x = (t.p3.x + t.p2.x) *.5;
          circle.y = m1 * (circle.x - mx1) + my1;  
        }
        else {
          m1 = - (t.p2.x-t.p1.x) / (t.p2.y-t.p1.y);
          m2 = - (t.p3.x-t.p2.x) / (t.p3.y-t.p2.y);
          mx1 = (t.p1.x + t.p2.x) * .5;
          mx2 = (t.p2.x + t.p3.x) * .5;
          my1 = (t.p1.y + t.p2.y) * .5;
          my2 = (t.p2.y + t.p3.y) * .5;
          circle.x = (m1 * mx1 - m2 * mx2 + my2 - my1) / (m1 - m2);
          circle.y = m1 * (circle.x - mx1) + my1;
        }

        dx = t.p2.x - circle.x;
        dy = t.p2.y - circle.y;
        rsqr = dx*dx + dy*dy;
        circle.z = sqrt(rsqr);

        dx = p.x - circle.x;
        dy = p.y - circle.y;
        drsqr = dx*dx + dy*dy;

        return drsqr &lt;= rsqr;
    }
}

//Calculates the intersection point between two line segments
//Port of Paul Bourke's C implementation of a basic algebra method

static class LineIntersector {

  //Epsilon value to perform accurate floating-point arithmetics
  static final float e = 1e-5;

  //Check intersection and calculates intersection point, storing it in a reference passed to the method
  static boolean intersect (float a_x1, float a_y1, float a_x2, float a_y2, 
                            float b_x1, float b_y1, float b_x2, float b_y2, 
                            PVector p) 
  { 
    //Check if lines are parallel
    float d  = ( (b_y2 - b_y1) * (a_x2 - a_x1) ) - ( (b_x2 - b_x1) * (a_y2 - a_y1) );
    if ( abs(d)&lt;e ) return false;    

    //Check if lines intersect
    float na, nb, ma, mb;
    na = ( (b_x2 - b_x1) * (a_y1 - b_y1) ) - ( (b_y2 - b_y1) * (a_x1 - b_x1) );
    nb = ( (a_x2 - a_x1) * (a_y1 - b_y1) ) - ( (a_y2 - a_y1) * (a_x1 - b_x1) );
    ma = na/d;
    mb = nb/d;
    if ( ma&lt;0 || ma&gt;1 || mb&lt;0 || mb&gt;1) return false;
    p.x = a_x1 + ( ma * (a_x2 - a_x1));
    p.y = a_y1 + ( ma * (a_y2 - a_y1));
    return true;
  }

  //We know both lines intersect, so don't check anything, only calculate the intersection point
  static PVector simpleIntersect (float a_x1, float a_y1, float a_x2, float a_y2, 
                                  float b_x1, float b_y1, float b_x2, float b_y2) 
  { 
    float 
    na = ( (b_x2 - b_x1) * (a_y1 - b_y1) ) - ( (b_y2 - b_y1) * (a_x1 - b_x1) ),
    d  = ( (b_y2 - b_y1) * (a_x2 - a_x1) ) - ( (b_x2 - b_x1) * (a_y2 - a_y1) ),
    ma = na/d;
    return new PVector (a_x1 + ( ma * (a_x2 - a_x1)), a_y1 + ( ma * (a_y2 - a_y1)));
  }
}


class Triangle 
{
    PVector p1, p2, p3;

    Triangle()  { p1 = p2 = p3 = null; }

    Triangle(PVector p1, PVector p2, PVector p3) {
        this.p1 = p1;
        this.p2 = p2;
        this.p3 = p3;
    }

    Triangle(float x1, float y1, float x2, float y2, float x3, float y3) 
    {
        p1 = new PVector(x1, y1);
        p2 = new PVector(x2, y2);
        p3 = new PVector(x3, y3);
    } 

    PVector center(){
        return LineIntersector.simpleIntersect(p1.x, p1.y, (p2.x + p3.x)*.5, (p2.y + p3.y)*.5, p2.x, p2.y, (p3.x + p1.x)*.5, (p3.y + p1.y)*.5);  
    }
}


/**
 *  ported from p bourke's triangulate.c
 *  <a href="http://astronomy.swin.edu.au/~pbourke/modelling/triangulate/" target="_blank" rel="nofollow">http://astronomy.swin.edu.au/~pbourke/modelling/triangulate/</a>
 *  fjenett, 20th february 2005, offenbach-germany.
 *  contact: <a href="http://www.florianjenett.de/" target="_blank" rel="nofollow">http://www.florianjenett.de/</a>
 */

class Triangulator 
{

    class XComparator implements Comparator&lt;PVector&gt; 
    {   
        public int compare(PVector p1, PVector p2) 
        {
            if      (p1.x &lt; p2.x) return -1;
            else if (p1.x &gt; p2.x) return  1;
                                  return  0;     
        }
    }

   class Edge 
   {
        PVector p1, p2;

        Edge() { p1 = p2 = null; }

        Edge(PVector p1, PVector p2) 
        {
            this.p1 = p1;
            this.p2 = p2;
        }
    }

    boolean sharedVertex(Triangle t1, Triangle t2) {
        return t1.p1 == t2.p2 || t1.p1 == t2.p2 || t1.p1 == t2.p3 ||
               t1.p2 == t2.p1 || t1.p2 == t2.p2 || t1.p2 == t2.p3 || 
               t1.p3 == t2.p1 || t1.p3 == t2.p2 || t1.p3 == t2.p3;
    }


    /**
      Triangulation subroutine
      Takes as input vertices (PVectors) in ArrayList pxyz
      Returned is a list of triangular faces in the ArrayList triangles 
      These triangles are arranged in a consistent clockwise order.
    */   
    void triangulate(List&lt;PVector&gt; pxyz, List&lt;Triangle&gt; triangles) 
    { 
      // sort vertex array in increasing x values
      Collections.sort(pxyz, new XComparator());

      // Find the maximum and minimum vertex bounds. This is to allow calculation of the bounding triangle
      float 
      xmin = pxyz.get(0).x,
      ymin = pxyz.get(0).y,
      xmax = xmin,
      ymax = ymin;

      for (PVector p : pxyz) {
        if (p.x &lt; xmin) xmin = p.x;
        else if (p.x &gt; xmax) xmax = p.x;
        if (p.y &lt; ymin) ymin = p.y;
        else if (p.y &gt; ymax) ymax = p.y;
      }

      float 
      dx = xmax - xmin,
      dy = ymax - ymin,
      dmax = dx &gt; dy ? dx : dy,
      two_dmax = dmax*2,
      xmid = (xmax+xmin) * .5,
      ymid = (ymax+ymin) * .5;

      HashSet&lt;Triangle&gt; complete = new HashSet&lt;Triangle&gt;(); // for complete Triangles

      /**
        Set up the supertriangle
        This is a triangle which encompasses all the sample points.
        The supertriangle coordinates are added to the end of the
        vertex list. The supertriangle is the first triangle in
        the triangle list.
      */
      Triangle superTriangle = new Triangle(xmid-two_dmax, ymid-dmax, xmid, ymid+two_dmax, xmid+two_dmax, ymid-dmax);
      triangles.add(superTriangle);

      //Include each point one at a time into the existing mesh
      ArrayList&lt;Edge&gt; edges = new ArrayList&lt;Edge&gt;();
      int ts;
      PVector circle;
      boolean inside;

      for (PVector p : pxyz) {
        edges.clear();

        //Set up the edge buffer. If the point (xp,yp) lies inside the circumcircle then the three edges of that triangle are added to the edge buffer and that triangle is removed.
        circle = new PVector();        

        for (int j = triangles.size()-1; j &gt;= 0; j--) 
        {     
          Triangle t = triangles.get(j);
          if (complete.contains(t)) continue;

          inside = CircumCircle.circumCircle(p, t, circle);

          if (circle.x+circle.z &lt; p.x) complete.add(t);
          if (inside) 
          {
              edges.add(new Edge(t.p1, t.p2));
              edges.add(new Edge(t.p2, t.p3));
              edges.add(new Edge(t.p3, t.p1));
              triangles.remove(j);
          }             
        }

        // Tag multiple edges. Note: if all triangles are specified anticlockwise then all interior edges are opposite pointing in direction.
        int eL = edges.size()-1, eL_= edges.size();
        Edge e1 = new Edge(), e2 = new Edge();

        for (int j=0; j&lt;eL; e1= edges.get(j++)) for (int k=j+1; k&lt;eL_; e2 = edges.get(k++)) 
            if (e1.p1 == e2.p2 &amp;&amp; e1.p2 == e2.p1) e1.p1 = e1.p2 = e2.p1 = e2.p2 = null;

        //Form new triangles for the current point. Skipping over any tagged edges. All edges are arranged in clockwise order.
        for (Edge e : edges) {
          if (e.p1 == null || e.p2 == null) continue;
          triangles.add(new Triangle(e.p1, e.p2, p));
        }    
      }

      //Remove triangles with supertriangle vertices
      for (int i = triangles.size()-1; i &gt;= 0; i--) if (sharedVertex(triangles.get(i), superTriangle)) triangles.remove(i);
    }

     /**
      Triangulation subroutine
      Takes as input vertices (PVectors) in ArrayList pxyz
      Returned is a list of triangular faces in the ArrayList triangles 
      These triangles are arranged in a consistent clockwise order.
    */   
    ArrayList&lt;Triangle&gt; triangulate(PVector[] vertices) 
    { 
      int len = vertices.length;

      // sort vertex array in increasing x values
      Arrays.sort(vertices, new XComparator());

      // Find the maximum and minimum vertex bounds. This is to allow calculation of the bounding triangle
      float 
      xmin = vertices[0].x,
      ymin = vertices[0].y,
      xmax = xmin,
      ymax = ymin;

      for (int i=0; i&lt;len; i++) {
        if      (vertices[i].x &lt; xmin) xmin = vertices[i].x;
        else if (vertices[i].x &gt; xmax) xmax = vertices[i].x;
        if      (vertices[i].y &lt; ymin) ymin = vertices[i].y;
        else if (vertices[i].y &gt; ymax) ymax = vertices[i].y;
      }

      float 
      dx = xmax - xmin,
      dy = ymax - ymin,
      dmax = dx &gt; dy ? dx : dy,
      two_dmax = dmax*2,
      xmid = (xmax+xmin) * .5,
      ymid = (ymax+ymin) * .5;

      ArrayList&lt;Triangle&gt; triangles = new ArrayList&lt;Triangle&gt;(); // for the Triangles
      HashSet&lt;Triangle&gt; complete = new HashSet&lt;Triangle&gt;(); // for complete Triangles

      /**
        Set up the supertriangle
        This is a triangle which encompasses all the sample points.
        The supertriangle coordinates are added to the end of the
        vertex list. The supertriangle is the first triangle in
        the triangle list.
      */
      Triangle superTriangle = new Triangle(xmid-two_dmax, ymid-dmax, xmid, ymid+two_dmax, xmid+two_dmax, ymid-dmax);
      triangles.add(superTriangle);

      //Include each point one at a time into the existing mesh
      ArrayList&lt;Edge&gt; edges = new ArrayList&lt;Edge&gt;();
      int ts;
      PVector circle;
      boolean inside;

      for (int v=0; v&lt;len; v++) {
        edges.clear();

        //Set up the edge buffer. If the point (xp,yp) lies inside the circumcircle then the three edges of that triangle are added to the edge buffer and that triangle is removed.
        circle = new PVector();        

        for (int j = triangles.size()-1; j &gt;= 0; j--) 
        {     
          Triangle t = triangles.get(j);
          if (complete.contains(t)) continue;

          inside = CircumCircle.circumCircle(vertices[v], t, circle);

          if (circle.x+circle.z &lt; vertices[v].x) complete.add(t);
          if (inside) 
          {
              edges.add(new Edge(t.p1, t.p2));
              edges.add(new Edge(t.p2, t.p3));
              edges.add(new Edge(t.p3, t.p1));
              triangles.remove(j);
          }             
        }

        // Tag multiple edges. Note: if all triangles are specified anticlockwise then all interior edges are opposite pointing in direction.
        int eL = edges.size()-1, eL_= edges.size();
        Edge e1 = new Edge(), e2 = new Edge();

        for (int j=0; j&lt;eL; e1= edges.get(j++)) for (int k=j+1; k&lt;eL_; e2 = edges.get(k++)) 
            if (e1.p1 == e2.p2 &amp;&amp; e1.p2 == e2.p1) e1.p1 = e1.p2 = e2.p1 = e2.p2 = null;

        //Form new triangles for the current point. Skipping over any tagged edges. All edges are arranged in clockwise order.
        for (Edge e : edges) {
          if (e.p1 == null || e.p2 == null) continue;
          triangles.add(new Triangle(e.p1, e.p2, vertices[v]));
        }    
      }

      //Remove triangles with supertriangle vertices
      for (int i = triangles.size()-1; i &gt;= 0; i--) if (sharedVertex(triangles.get(i), superTriangle)) triangles.remove(i);

      return triangles;
    }
}
</code></pre>
]]></description>
   </item>
   </channel>
</rss>