toxiclibs - how to calculate the distance from a Plane

edited August 2015 in Library Questions

Hello, I have written these lines of code:

Vec3D target, camPosition, myPosition;
...
...
Plane fPlane = new Plane(target, camPosition);
float distToFPlane = fPlane.getDistanceToPoint(myPosition);

They work perfectly but I'd like to know how to do the same thing without using toxiclibs.

Can you tell me how to do the same calculations only with Processing objects (as PVector) ?

Answers

  • edited August 2015

    Just use PVector.dist(). in the code you posted you specified target, camPosition and myPosition. your plane is created with target and camPosition (I think you want new Plane(camPosition, target) as it is _Plane(Vec3D origin, ReadonlyVec3D norm) _). If you use Plane.getDistanceToPoint(), i think the only distance you can get is the distance between the plane origin and myPosition, which is just measuring the distance between to points. so just write:

    PVector camPosition = new PVector();
    PVector myPosition = new PVector();
    float dist = PVector.dist(camPosition,myPosition);
    
  • edited August 2015

    Hello, it's not the same thing: you propose to calculte the distance between two vectors. I want to calculate the distance between a vector and a plane.

    I tried this solution before and the results are not the same.

    I need to create my own simplified Plane class and getDistanceToPoint function but I don't know exactly how to use camPosition in relation with target (in my example).

    But you are right about the constructor, I'm trying to rewrite this one:

    Plane(Vec3D origin, ReadonlyVec3D norm)

    http://toxiclibs.org/docs/core/toxi/geom/Plane.html

  • edited August 2015 Answer ✓

    Ah, I think now i understand. the toxi plane method measures in the direction of the normal. than it is simple trigonometry:

    import toxi.geom.*;
    
    PVector origin, normal, pos;
    Vec3D toxiOrigin, toxiNormal, toxiPos;
    Plane plane;
    float dist, toxiDist;
    
    void setup(){
      size(800,800,P3D);
      origin = new PVector(100,100,0);
      normal = new PVector(1,0,0);
      pos = new PVector(700,700,0);
    
      toxiOrigin = new Vec3D(100,100,0);
      toxiNormal = new Vec3D(1,0,0);
      toxiPos = new Vec3D(700,700,0);
      plane = new Plane(toxiOrigin,toxiNormal);
    
      dist = getDist(origin,normal,pos);
      toxiDist = plane.getDistanceToPoint(toxiPos);
    
      println("toxiDist = " + toxiDist);
      println("pvectorDist = " + dist);
    }
    
    public float getDist(PVector origin,PVector normal,PVector pos){
      PVector hypotenuse = PVector.sub(pos,origin);
      float c = hypotenuse.mag();
      hypotenuse.normalize();
      float alpha = PVector.angleBetween(normal,hypotenuse);
    
      return cos(alpha) * c;
    }
    
  • edited August 2015

    Thank you! I need to revise trigo indeed ;)

  • I have found a fastest method :

    public float getDist(PVector origin,PVector normal,PVector pos){
    
        PVector hypotenuse = PVector.sub(pos,origin);
        float c = hypotenuse.mag();
    
        hypotenuse.normalize();
        normal.normalize();
    
        float cos = PVector.dot(normal, hypotenuse);
    
        return cos*c;
    }
    
  • edited August 2015

    (double post)

Sign In or Register to comment.