Last active
October 22, 2020 19:34
-
-
Save villares/af485db805881af5964063587dfe8a0f to your computer and use it in GitHub Desktop.
FreyaHolmer's Mathfs ported to Processing Java!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // https://twitter.com/FreyaHolmer/status/1319278214087258112?s=20 | |
| // FROM: https://github.com/FreyaHolmer/Mathfs/ | |
| /* | |
| TODO: YEAH INTERSECT IT IS HARD... IF ANYONE WANTS TO HELP...! | |
| - CircleFromThreePoints() depends on LineSegment2D and Intersect... | |
| - I have some lazy line segment intersection I could use | |
| but Freya's has cool Rays and infinite lines... | |
| - ... | |
| - Then I'll port everything to Processing Python mode... | |
| */ | |
| class Circle { | |
| PVector center; | |
| float radius; | |
| Circle( PVector center, float radius ) { | |
| this.center = center; | |
| this.radius = radius; | |
| } | |
| Circle(float x, float y, float radius) { | |
| this.center = new PVector(x, y); | |
| this.radius = radius; | |
| } | |
| void draw() { | |
| circle(center.x, center.y, radius * 2); | |
| } | |
| float Area() { | |
| return RadiusToArea(radius); | |
| } | |
| float Circumference() { | |
| return RadiusToCircumference( radius ); | |
| } | |
| float RadiusToArea( float r ) { | |
| return r * r * ( 0.5f * TAU ); | |
| } | |
| float AreaToRadius( float area ) { | |
| return sqrt( 2 * area / TAU ); | |
| } | |
| float AreaToCircumference( float area ) { | |
| return sqrt( 2 * area / TAU ) * TAU; | |
| } | |
| float CircumferenceToArea( float c ) { | |
| return c * c / ( 2 * TAU ); | |
| } | |
| float RadiusToCircumference( float r ) { | |
| return r * TAU; | |
| } | |
| float CircumferenceToRadius( float c ) { | |
| return c / TAU; | |
| } | |
| } | |
| //Circle CircleFromThreePoints( PVector a, PVector b, PVector c) { | |
| // Line2D lineA = LineSegment2D.GetBisectorFast( a, b ); | |
| // Line2D lineB = LineSegment2D.GetBisectorFast( b, c ); | |
| // PVector center = Intersect.Lines( lineA, lineB); | |
| // if (center != null) { | |
| // return new Circle(center, PVector.dist(center, a ); | |
| // ); | |
| // } | |
| // return null; | |
| //} | |
| Circle CircleFromTwoPoints( PVector a, PVector b ) { | |
| return new Circle(PVector.add(a, b).div(2.0), PVector.dist( a, b ) / 2.0); | |
| } | |
| Circle CircleFromTwoPoints(float xa, float ya, float xb, float yb ) { | |
| return CircleFromTwoPoints( new PVector(xa, ya), new PVector(xb, yb)); | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 2D line math | |
| class Line2D { | |
| PVector origin; | |
| PVector dir; | |
| Line2D( PVector origin, PVector dir ) { | |
| this.origin = origin; | |
| this.dir = dir; | |
| } | |
| PVector GetPoint( float t ) { | |
| return origin.add(dir.mult(t)); | |
| } | |
| /// Projects a point onto an infinite line | |
| /// "lineOrigin">Line origin | |
| /// "lineDir">Line direction (does not have to be normalized) | |
| /// "point">The point to project onto the line | |
| PVector ProjectPointToLine( PVector lineOrigin, PVector lineDir, PVector point ) { | |
| PVector coord = point.sub(lineOrigin); | |
| float t = PVector.dot( lineDir, coord ) / PVector.dot( lineDir, lineDir ); | |
| return lineOrigin.add(lineDir.mult(t)); | |
| } | |
| /// Projects a point onto an infinite line | |
| /// "line">Line to project onto | |
| /// "point">The point to project onto the line | |
| PVector ProjectPointToLine( Line2D line, PVector point ) { | |
| return ProjectPointToLine( line.origin, line.dir, point ); | |
| } | |
| /// Returns the signed distance to a 2D plane | |
| /// "planeOrigin">Plane origin | |
| /// "planeNormal">Plane normal (has to be normalized for a true distance) | |
| /// "point">The point to use when checking distance to the plane | |
| float PointToPlaneSignedDistance( PVector planeOrigin, PVector planeNormal, PVector point ) { | |
| return PVector.dot( point.sub(planeOrigin), planeNormal ); | |
| } | |
| /// Returns the distance to a 2D plane | |
| /// "planeOrigin">Plane origin | |
| /// "planeNormal">Plane normal (has to be normalized for a true distance) | |
| /// "point">The point to use when checking distance to the plane | |
| float PointToPlaneDistance( PVector planeOrigin, PVector planeNormal, PVector point ) { | |
| return abs( PointToPlaneSignedDistance( planeOrigin, planeNormal, point ) ); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| void setup(){ | |
| size(400, 400); | |
| Circle c0 = new Circle(100, 100, 100); | |
| c0.draw(); | |
| // Circle c1 = CircleFromTwoPoints(new PVector(100, 100), new PVector(200, 200)); | |
| Circle c1 = CircleFromTwoPoints(100, 100, 200, 200); | |
| c1.draw(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Work in progres... In Java I don't have "out" parameters (reminds me of VAR in Pascal!).
Vector2 -> PVector
I'm going with
null(non-intersecting) orPVector(intersection), I guess.