computing the intersection point of an ellipse and a line in Processing - java

I'm having trouble computing the intersection point of an ellipse and a line.
Let's say I have an ellipse at point (0/0) with radius 500. Additionaly I'm drawing a line from point (0/0) to (mouseX, mouseY).
First I check if the mouse coordinates are outside the ellipse by doing
if((mouseX*mouseX)+(mouseY*mouseY)) > 500*500){/*...*/}
Now, whenever the mouse coordinates are outside that ellipse, I want to draw the line not until the mouse, but until the 'border' of the ellipse. In order to do so, I must have the intersection point of the line and the ellipse.
Are there any libraries that simplify such trigonometric tasks? Is there any other more or less easy way to compute the intersection?

From what you've said, I'm making the following assumptions:
The ellipse is always circular (same radius all the way round).
The line is always being drawn from the centre of the circle.
If those are true then the problem is actually very simple. All you need to do is truncate the line so that its length is the same as the circle's radius, and that gives you the intersection point.
If the mouse is outside the ellipse:
Store the vector describing the line; in this case (mouseX, mouseY).
Normalize the vector (divide each component by the length of the line).
Multiply the vector by the radius of the circle.
The vector now contains the intersection point, relative to the centre of the circle.
You don't have to use a vector class for this, although it might help.
If your circle and line aren't starting on the origin (0,0) then you'll need to compensate. At step 1, subtract your new origin from (mouseX,mouseY). After step 3, add the origin back in to get the display coordinates.

Related

calculating angled distances across a square

Given a point in a square I need to calculate how far that point is across the square (0% progress at one end, 100% at the other). For simple horizontal (or vertical) traversals the progress is just the X-coordinate (or Y-coordinate) divided by the size of the square.
But here's where I'm struggling: I need the traversal to be at an angle, so a few (degrees? radians?) either slanting up or down. If this helps: all the points with matching "progress" values, should form a line perpendicular to the path/angle of traversal.
I need to write a method like this (most any language), that returns a value between 0 and 1:
float calculateProgress(float x, float y, float size, float angle) { }
... but not sure where to start.
Any help greatly appreciated!
Edit/Added Description: for a flat/horizontal traversal, the points that report "50% progress" would form a vertical line in the middle of the square. But rotated a little counter-clockwise, I want those same points (now rotated to form a slanting line) to report "50% progress" even though the top half are now to the left of x/size and the bottom half are now to the right of x/size.

Collision Polygon and Circle - Lingdx

I want to detect when a polygon and a Circle collide. Other possibility that i have think is with rectangle, but it can't be possible because i want to rotate the rectangle, so what is the solution?
Thank you
There is Intersector class in LibGDX, it should work just fine for your needs.
libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/math/Intersector.html
Use intersectSegmentCircle method.
It does take Vector2 variables as arguments, so you have to use Vector2 for storing x and y coordinates of your points - circle center and polygon vertices.
There are also Polygon and Circle classes, you can use those for storing coordinates as Vector2.
Anyway, lets assume you already have circle and polygon described by set of Vector2 points:
Vector2 circleCenter, PolyVertex1, PolyVertex2, PolyVertex3, ..., PolyVertexN;
There are line segments between polygon vertices, you have to check, if any of those segments intersects with circle. So, for every pair of verices, check, if:
intersectSegmentCircle(PolyVertex1, PolyVertex2, circleCenter, radius^2);
intersectSegmentCircle(PolyVertex2, PolyVertex3, circleCenter, radius^2);
...
intersectSegmentCircle(PolyVertexN-1, PolyVertexN, circleCenter, radius^2);
intersectSegmentCircle(PolyVertexN, PolyVertex1), circleCenter, radius^2);
returns true. If yes, it means your polygon and circle colided.
In some cases (small circle and large polygon), there can be no intersection between polygon edges and circle, whole circle can be inside polygon. Then you will have to use isPointInPolygon() method. It takes Array of Vector2 (polygon vertices) and Vector2 (cirlce center) as arguments.
So, store polygon vertices in array, and check if isPointInPolygon returns true. If yes, then again, there is collision.
Array <Vector2> PolyVertices;
...
isPointInPolygon(PolyVertices,circleCenter);
Dont copy&paste above code, wont work because of lack of initialization and thelike ;) But i hope, idea is clear.

Detect collisions between circles with rectangles inside

I'm working on a project, and I need to be able to detect collisions between circles. I already found a mathematical formula for that : http://cgp.wikidot.com/circle-to-circle-collision-detection
But I've got a question, how can I detect if there is a rectangle in this area ? Or just a part of a rectangle inside ?
I've got : coordinates of the center of a circle and the radius, and for the rectanble I've got a x and y coordinate, and width an height. I guess that x and y are just a point and with that I'm able to guess the form with the width and the height.
Any idea ?
Thanks a lot !
Write a method to check whether a point lies within a circle or not.
Call that method for all corner points of the rectangle (calculated from x, y, width and height) on both circles.
Use your existing circle intersection detector method to prune calls.
Hope this helps.
Good luck.
You can use java.awt.geom.Area class.
It has a constructor to create an Area from Shape
public Area(Shape s)
So create simple areas for your circles and rectangle. Then you can either combine areas using the methods to obtain new areas.
public void add(Area rhs)
public void subtract(Area rhs)
And check whether area intersects or contains another area via
public void intersect(Area rhs)
public boolean contains(Rectangle2D r)
This sounds like a variation of the technique described in this answer.
The same two cases apply (either the circle's centre is in the region, or one or more of the rectangle's edges intersects the region)... the difference is that instead of considering the circle in general, you need to consider the intersection of the circle.
The first case is easy because you can swap centre points. If the rectangle's centre point is in the intersection of the circles, then the rectangle is partly inside. This is easy to determine: find the centre point of the rectangle, see if it's in the first circle, see if it's in the second circle.
The second case is complicated, because it requires you to calculate the curves where the circles intersect. If the edges of the rectangle intersect either of those curves then the rectangle overlaps the intersection. As a special case, if one circle lies completely inside the other one, then the line to check is the border of the smaller circle.
If you don't need an exact answer, then the second case can be approximated. First, find the points where the two circles intersect (or use the method you've already come up with, if you can). These two points can be used to construct a bounding rectangle (they are either the top left/bottom right or top right/bottom left points of a rectangle). If this bounding rectangle intersects with your rectangle, then your rectangle probably overlaps the circle intersection.
All in all, this is fairly complicated if you want to an exact answer that works properly with all of the special cases (one circle completely inside the other, the rectangle intersects both circles but not their intersection, etc). I hope this helps a little.
A library I've used before called the JTS topology suite might be appropriate for your needs. It's orientated more towards GIS operations than pure euclidean geometry, but it can easily do all of these calculations for you once you've got the shapes defined:
import com.vividsolutions.jts.util.*
GeometricShapeFactory gsf = new GeometricShapeFactory();
gsf.setSize(100);
gsf.setNumPoints(100);
gsf.setBase(new Coordinate(100, 100));
//configure the circle as appropriate
Polygon circleA = gsf.createCircle();
//configure again and create a separate circle
Polygon circleB = gsf.createCircle();
//configure a rectangle this time
Polygon rectangle = gsf.createRectangle();
Geometry circleIntersection = circleA.intersection(circleB);
return rectangle.intersects(circleIntersection);

Calculate shape orientation in Java (Image analysis)

I have an image such as this:
and I need to calculate the orientation of it. In this case the shape is pointing towards the top left of the screen. Accuracy isn't hugely important as long as 3 or 4 calculations average out to within 5 degrees or so of the actual orientation (it will be moving slightly).
Can anyone point me towards an algorithm to do this? I don't mind if the orientation is returned as a double or as a vector.
If the image is always T-shaped, you can simply get the furthest pair of pixels, then find the furthest pair from either both of those (the edges of the T), find which is further from the other two, draw a line from that one to the middle point of those two.
You can further refine it by then finding the base of the T by comparing the middle line with the edges of the base, and adjusting the angle and offset until it is actually in the middle.
The definitive solution is impossible I guess, since requires image recognition. I would project the 2D image onto axis, i.e. obtain the width and height of the image and get direction vector from these values taking them as components.
First, a couple of assumptions:
The center and centroid are "close"
The descending bar of the T is longer than the cross-bar
First, determine the bounding rectangle of the image and find the points of the image that lie along this rectangle. For points that lie along the line and are a certain distance from one another (say 5 pixels to pick a value) you'll need to only take 1 point from that cluster. At the end of this you should have 3 points, i.e. a triangle. The shortest side of the triangle should be the cross-bar (from assumption 2), i.e. find the two points closest to each other. The line that is perpendicular to the line crossing those two points is then your orientation line, i.e. find the angle between it and the horizontal axis.
I would try morphological skeletonization to simplify the image, followed by some straightforward algorithm to determine the orientation of the longer leg of the skeleton.
The solution in the end was to use a Convex Hull Algorithm, which finds the minimum number of points needed to enclose a shape with a bound.

Java - filling an overlapping polygon

I'm trying to draw a 5 point star in AWT.
Each point in the 2d grid is 72 degrees apart - so I thought I could draw the polygon using only 5 points by ordering the points 144 degrees apart, so the polygon gets fed the points in order 1,3,5,2,4
Unfortunately, this involves a lot of intersecting lines, and the end result is that there are 5 triangles with my desired colour, circling a pentagon that has not been coloured.
Looking through, it has something to do with an even-odd rule, that intersecting points will not be filled.
I need my star to be drawn dynamically, and using the specific shape described (for scaling and such).
If I manually plot the points where it intersects, I get some human error in my star's shape.
Is there any way to just turn this feature off, or failing that, is there a way to get the polygon to return an array of x[] and y[] where lines intersect so I can just draw another one inside it?
Thanks.
Draw it with ten points, 36 degrees apart, at two alternating radii.
Establish the 10-point Polygon in cartesian coordinates, as suggested by relet and as shown in this example. Note how the coordinate system is centered on the origin for convenience in rotating, scaling and translating. Because Polygon implements the Shape interface, the createTransformedShape() method of AffineTransform may be applied. A more advanced shape library may be found here.
Is there a way to get the polygon to return an array of x[] and y[] where lines intersect?
Although typically unnecessary, you can examine the component coordinates using a Shape's PathIterator. I found it instructive to examine the coordinates before and after invoking createTransformedShape().

Categories

Resources