• fullscreen
  • triCentroid.pde
  • triangle_centroid.pde
  • /**
     *   fjenett, february 21st 2005, offenbach-germany
     *   updated 2009-04 for processing 1.0.3.
     *   .. and added centroid dragging.
     *
     *   calculates the centroid of a triangle, probably based on code by p bourke.
     */
    
    XY a,b,c;
    int rw = 5, xo, yo;
    
    XY[] tri;
    XY triDragged, centroid;
    
    float twothird = 2.0/3.0;
    
    void setup ()
    {
        size(200,200);
        
        a = new XY(random(width),10);
        b = new XY(random(width),height/2);
        c = new XY(random(width),height-10);
        tri = new XY[]{a, b, c};
        
        centroid = new XY(-1,-1);
        
        rectMode( CENTER );
        smooth();
    }
    
    void draw ()
    {
        background(120);
        beginShape(TRIANGLES);
        vertex(a.x,a.y);
        vertex(b.x,b.y);
        vertex(c.x,c.y);
        endShape();
        for (int i=0; i<tri.length; i++) rect(tri[i].x, tri[i].y, rw,rw);
    
        XY bc = new XY(b.x+(c.x-b.x)/2, b.y+(c.y-b.y)/2);
        centroid.set( int(a.x+(bc.x-a.x)*twothird) , int(a.y+(bc.y-a.y)*twothird) );
    
        ellipse(centroid.x, centroid.y, rw,rw);
    }
    
    void mousePressed ()
    {
        triDragged = null;
        for (int i=0; i<tri.length; i++)
            if (tri[i].inside(mouseX, mouseY)) {
                triDragged = tri[i];
            }
        if ( triDragged == null && centroid.inside(mouseX, mouseY) )
            triDragged = centroid;
    }
    
    void mouseDragged()
    {
        if ( triDragged != null )
        {
            triDragged.x = mouseX;
            triDragged.y = mouseY;
            if ( triDragged == centroid )
            {
                for (int i=0; i<tri.length; i++)
                {
                    tri[i].x += mouseX-pmouseX;
                    tri[i].y += mouseY-pmouseY;
                }
            }
        }
    }
    
    class XY
    {
        float x, y;
        
        XY (float xx, float yy)
        {
            x=xx; 
            y=yy;
        }
        
        boolean inside (int xx, int yy)
        { 
            return (xx>x-5 && xx<x+5 && yy>y-5 && yy<y+5); 
        }
        
        void set ( int _x, int _y )
        {
            x=_x; 
            y=_y;
        }
    }
    
    /**
     *   fjenett, february 21st 2005, offenbach-germany
     *   updated 2009-04 for processing 1.0.3.
     *
     *   calculates the centroid of a triangle, probably based on code by p bourke.
     */
    
    XY a,b,c;
    int rw = 5;
    
    XY[] tri;
    XY triDragged;
    
    float twothird = 2.0/3.0;
    
    void setup ()
    {
        size(200,200);
        
        a = new XY(random(width),10);
        b = new XY(random(width),height/2);
        c = new XY(random(width),height-10);
        tri = new XY[]{a, b, c};
        
        rectMode( CENTER );
        smooth();
    }
    
    void draw ()
    {
        background(120);
        beginShape(TRIANGLES);
        vertex(a.x,a.y);
        vertex(b.x,b.y);
        vertex(c.x,c.y);
        endShape();
        for (int i=0; i<tri.length; i++) rect(tri[i].x, tri[i].y, rw,rw);
    
        XY bc = new XY(b.x+(c.x-b.x)/2, b.y+(c.y-b.y)/2);
        XY centroid = new XY( int(a.x+(bc.x-a.x)*twothird) , int(a.y+(bc.y-a.y)*twothird) );
    
        ellipse(centroid.x, centroid.y, rw,rw);
    }
    
    void mousePressed ()
    {
        triDragged = null;
        for (int i=0; i<tri.length; i++)
            if (tri[i].inside(mouseX, mouseY)) {
                triDragged = tri[i];
            }
    }
    
    void mouseDragged()
    {
        if ( triDragged != null )
        {
            triDragged.x = mouseX;
            triDragged.y = mouseY;   
        }
    }
    
    class XY
    {
        float x, y;
        
        XY (float xx, float yy)
        {
            x=xx; 
            y=yy;
        }
        
        boolean inside (int xx, int yy)
        { 
            return (xx>x-5 && xx<x+5 && yy>y-5 && yy<y+5); 
        }
    }
    
    /*
    public String findCentroid()
     {
     int[] x = triangle.xpoints;
     int[] y = triangle.ypoints;
     // construct the medians defined as the line from
     // any vertex to the midpoint of the opposite line
     medians = new Line2D[x.length];
     for(int j = 0; j < x.length; j++)
     {
     int next = (j + 1) % x.length;
     int last = (j + 2) % x.length;
     Point2D vertex = new Point2D.Double(x[j], y[j]);
     // get midpoint of line opposite vertex
     double dx = ((double)x[last] - x[next])/2;
     double dy = ((double)y[last] - y[next])/2;
     Point2D oppLineCenter = new Point2D.Double(x[next] + dx,
     y[next] + dy);
     medians[j] = new Line2D.Double(vertex, oppLineCenter);
     }
     // centroid is located on any median 2/3 the way from the
     // vertex (P1) to the midpoint (P2) on the opposite side
     double[] lengths = getSideLengths();
     double dx = (medians[0].getX2() - medians[0].getX1())*2/3;
     double dy = (medians[0].getY2() - medians[0].getY1())*2/3;
     double px = medians[0].getX1() + dx;
     double py = medians[0].getY1() + dy;
     //System.out.println("px = " + nf.format(px) +
     //                 "\tpy = " + nf.format(py));
     centroid = new Point2D.Double(px, py);
     return "(" + nf.format(px) + ",  " + nf.format(py) + ")";
     }
     */
    
    
    
    

    code

    tweaks (0)

    about this sketch

    This sketch is running as Java applet, exported from Processing.

    license

    advertisement

    Florian Jenett

    triangle centroid

    Add to Faves Me Likey@!
    You must login/register to add this sketch to your favorites.

    from my 2004 sketchbook: calcualte the centroid of a triangle. code may be based on code by <a href="http://local.wasp.uwa.edu.au/~pbourke/geometry/polyarea/">paul bourke</a>, not sure tough.

    click and drag the corners to see centroid being updated ..

    Myer Nore
    7 Apr 2009
    Nice! I love your elegant code. I wonder if it could be done backwards - move the centroid, and adjust the triangle.
    Florian Jenett
    7 Apr 2009
    added ... you might need to restart your browser to clean the java cache.
    You need to login/register to comment.