This is my solution to this question:
Given a circle represented as (radius, x_center, y_center) and an
axis-aligned rectangle represented as (x1, y1, x2, y2), where (x1, y1)
are the coordinates of the bottom-left corner, and (x2, y2) are the
coordinates of the top-right corner of the rectangle.
Return True if the circle and rectangle are overlapped otherwise
return False.
In other words, check if there are any point (xi, yi) such that
belongs to the circle and the rectangle at the same time.
class Solution {
public boolean checkOverlap(int radius, int x_center, int y_center, int x1, int y1, int x2, int y2) {
for(int i=x1; i<=x2; ){
for(int j=y1; j<=y2; ){
System.out.println((Math.pow(i-x_center, 2) +" "+ Math.pow(j-y_center, 2))+" "+Math.pow(radius, 2));
System.out.println(i+" "+j);
if((Math.pow(i-x_center, 2)+Math.pow(j-y_center, 2))<=Math.pow(radius, 2)){
return true;
}
j += 1;
}
i += 1;
}
return false;
}
}
I am pretty confident that the logic is correct. Ranging from the bottom left corner of the rectangle to the top right point, for every point, I am checking if it lies inside the circle.
If I increase the increment step to anything beyond '1', I see that the code fails for the test cases where the rectangle and circle just 'touch' each other. But having it this way leads to exceeding the time limit in some cases. How do I optimize the code for this logic?
Thanks in advance.
This problem can be simplified. I've found a solution of time complexity O(1) and memory complexity O(1). Instead of checking for every single pixel from the rectangle, you can even only take into consideration the bounds themselves.
As I see it, there are 3 cases:
Circle is fully inside rectangle.
Rectangle is fully inside circle
Circle and rectangle outlines intersect in at least one point. This is the tougher one.
I'll call center of the circle coordinates x0 and y0.
You can simply check the bounding box for the circle, as in, which is the northern most point of the circle(x0,y0-radius), which is the southern-most point of the circle(x0,y0+radius), eastern-most(x0-radius,y0) and western-most(x0+radius,y0). If they all fall within the rectangle, then problem solved.
If a rectangle is fully within a circle, that definitely means that its corners are at a smaller distance from the center of the circle than the radius. Simply check this distance for each corner.
Now comes the hard part.
So, as you have figured out, being in a circle(or intersecting one) means that some point must be at a smaller or equal distance from the center than the radius.
However, we can do an optimization for the rectangle checking part, too. Intersection with a rectangle likely means intersection with at least one of the segments that make the outline of the rectangle. So, in the case of an intersection between a rectangle and a circle, you need to check if there is any segment that would be at a smaller or equal distance from the center of the circle than the radius.
Edit: Also, the reason that your code fails on tests where they barely touch is likely due to floating-point errors. Do not use == (or in this case <=, which is similar) for checking if two floating point(or even a floating-point and integer) values are the same. Math.pow() in Java returns double. Just use normal multiplication for squaring.
In fact, you might want to stay as far from floating-point as possible, unless you can't figure out how to get rid of them and the problem says "0.001 error is acceptable" or something around the lines. They are both slow and prone to errors.
Edit 2: Also, I've written the code to help you aid in understanding this explanation. I've tested it on the site, it works for every test with runtime 1ms and memory usage 37.7Mb.
class Solution {
public boolean checkOverlap(int radius, int x_center, int y_center, int x1, int y1, int x2, int y2) {
//case 1: circle is fully inside rectangle
//check the bounding box of the circle against the rectangle
if(x_center-radius>=x1&&y_center-radius>=y1
&&x_center+radius<=x2&&y_center+radius<=y2
)
return true;
//case 2: checking closest corner against circle bounds
int minX=(Math.abs(x_center-x1)>Math.abs(x_center-x2))?(x_center-x2):(x_center-x1);
int minY=(Math.abs(y_center-y1)>Math.abs(y_center-y2))?(y_center-y2):(y_center-y1);
if(minX*minX+minY*minY<=radius*radius)
return true;
//case 3: checking distances to segments against circle bounds
//Checking distance from a segment to a point is alike checking
//the distance from the line the segment is part of to a point,
//except you have to check if the closest point from the segment
//is actually on the segment. If it isn't, the distance from a
//segment to a point is the minimum distance from one of its
//corners to the point.
if(x1<=x_center&&x_center<=x2)
if(minY*minY<=radius*radius)
return true;
if(y1<=y_center&&y_center<=y2)
if(minX*minX<=radius*radius)
return true;
return false;
}
}
This code could be possibly shortened. However, time complexity-wise, it can't get better than O(1).
Related
I'm writing a very basic raycaster for a 3D scene with triangulated objects and everything worked fine until I decided to try casting rays from points other than the origin of the scene (0/0/0).
However, when I changed to origin of the ray to (0/1/0) the intersection test suddenly returned a wrong intersection point for one of the triangles.
I'm deliberately "shooting" the rays into the direction of the center of the triangle, so obviously this should be the intersection point. I just simply don't know what's exactly leading to the wrong results in my code.
(I'm not using Möller-Trumbore at the moment because I'd like to start out with a simpler, more basic approach, but I will switch to Möller-Trumbore when optimizing the code.)
These are the coordinates of my the three vertices of the above mentioned triangle:
-2.0/2.0/0.0 | 0.0/3.0/2.0 | 2.0/2.0/0.0
This is the center of the triangle:
0.0/2.3333333333333335/0.6666666666666666
This is my ray (origin + t * Direction):
Origin: 0.0/1.0/0.0
Direction (normalized): 0.0/0.894427190999916/0.4472135954999579
This is the obviously wrong intersection point my program calculated (before checking and finding out that the point is not even on the triangle:
0.0/5.0/1.9999999999999996
So yeah, it's not hard to see (even without a calculator) that the ray should hit the triangle at its center at roughly t = 1.5. My code, however, returns the value 4.472135954999579 for t.
Here's my code for the intersection check:
public Vector intersectsWithTriangle(Ray ray, Triangle triangle) {
boolean intersects = false;
Vector triangleNormal = triangle.getNormalVector();
double normalDotRayDirection = triangleNormal.dotProduct(ray.getDirection());
if(Math.abs(normalDotRayDirection) == 0) {
// parallel
return null;
}
double d = triangleNormal.dotProduct(triangle.getV1AsVector());
double t = (triangleNormal.dotProduct(ray.getOrigin()) + d) / normalDotRayDirection;
// Check if triangle is behind ray
if (t < 0) return null;
// Get point of intersection between ray and triangle
Vector intersectionPoint = ray.getPosAt(t);
// Check if point is inside the triangle
if(isPointInTriangle(intersectionPoint, triangle, triangleNormal)) {
intersects = true;
return intersectionPoint;
}
return null;
}
Any ideas what's wrong with the line that calculates t?
If the ray is given by o + t*v and the triangle plane is defined by normal vector n and point p, then we are looking for t s.t. n*(o + t*v) = n*p which gives t = (n*p - n*o)/(n*v). So you seem to have a sign error and the correct computation for t should be:
double t = (d - triangleNormal.dotProduct(ray.getOrigin())) / normalDotRayDirection;
As long as the ray origin was (0,0,0) the wrong sign did not matter.
I am trying to figure out how to implement the following method in java;
** Point rot90()** Query for a new Cartesian Point equivalent to this Cartesian point rotated by 90 degrees
I have no idea how to go about creating this method. However, I do believe that pulling the point (x,y) and outputting new point (y,x*-1) is equivalent to rotating 90 degrees. Basically the old y coordinate becomes the nee x coordinate and the new y coordinate is the old x coordinate multiplied by negative 1. Any thoughts on how to set up this method would be greatly appreciated. Thanks.
this is what I have so far
public Point rot90(){
Point rotated = new Point();
rotated.yCoord = xCoord*-1;
rotated.xCoord = yCoord;
return rotated;
}
I know this doesn't work as pointed out when I try to compile it. Any suggestions?
Your method needs an argument.
public Point rot90(Point p){
Point rotated = new Point();
rotated.yCoord = -p.xCoord;
rotated.xCoord = p.yCoord;
return rotated;
}
If your Point class has a constructor that can take coordinates then you can make it shorter.
public Point rot90(Point p){
return new Point(p.yCoord, -p.xCoord);
}
What exactly is the problem? Code not working or the result not as expected?
In the complex plane interpretation of Euclidean geometry, rotation (x,y) by 90° is, by definition of the complex unit, the multiplication of x+i·y by i. Since
i·(x+i·y)=-y+i·x,
the rotated point has the coordinates (-y,x), and your code implements the rotation by 270° =^= -90°.
In general, the rotation by an angle a amounts to the multiplication
(cos(a)+i*sin(a)) * (x+i*y)
Note that the screen coordinate system is the mirror image of the Cartesian plane, the y axis points down. Thus angle orientation is reversed.
What I'm trying to do is basically the thing you can do in the desktop when you click and drag te mouse making a square. The problem is I don't know how to make it draw "backwards" or how to clean the previous parameters when you start a new square. here is the entire code:
public void paint (Graphics j){
super.paint(j);
j.drawRect(x,y,z,w);
}
private void formMousePressed(java.awt.event.MouseEvent evt) {
x=evt.getX();
y=evt.getY();
repaint();
}
private void formMouseDragged(java.awt.event.MouseEvent evt) {
z=evt.getX();
w=evt.getY();
repaint();
}
The signature for drawRect is: drawRect(int x, int y, int width, int height). You need to calculate the top left corner of the square, and the width and height.
The top-left corner is (min(x, z), min(y, w)).
The width is abs(x-z) and the height is abs(y-w)
Putting this together we get
Try
j.drawRect(Math.min(x, z), Math.min(y, w), Math.abs(x - z), Math.abs(y - w));
Why does this work? Well you're given 2 points. It's a well known fact that 2 points can determine a square(opposite corners). The first problem is that you have to translate the points you're given, into an input that java likes. In this case, you first need the upper left hand corner. You don't know which point you have is that corner, or actually it could be that neither of them are.
So what do we know about the upper left corner? We know that it's x value is the smallest x value that exists in the square. We also know that at least one of the 2 points given rest on that same edge. Using this information we can determine that the x coordinate of the top left corner is the smallest x value of our 2 points. Or min(x, z). We use the same procedure to find the y coordinate.
Now width and height are easy. The width is the right edge - the left edge. We don't know which point is the right side, and which is the left side, but it doesn't matter. If we take the absolute value of the difference will always give you the positive difference between the points. In this case abs(x-z). The process is the same for the height.
As for resetting the square try adding a formMouseReleased method and setting x, y, z, w to 0.
I think you might create a method that resets the parameters
something like: void modifyMouse() in your Mouse class
//your parameters=0
I might try to give you a better help if you clarify your question, for now try that.
I'm making pong in Java and wanted to make the game more fun by assigning different reflection logic to each part of the paddle, like so:
(ball hittins outter edges of paddle will have a different effect than it hitting the middle of the paddle)
The paddle extends Rectangle2D so I could use Rectangle2D's intersects() method to determine if the ball has touched any part of it...
Is it possible to determine where exactly the ball has hit on the paddle?
What I'm planning to do is,
calculate angle of incidence and reflective angle based on that...
If the ball hits at a point x on the paddle... I will change the reflection angle accordingly
Thanks
Is it possible to determine where exactly the ball has hit on the paddle?
If it were me, I would grab the current co-ordinates of both the ball and the paddle. For the paddle, you can get two sets of y co-ordinates, to describe the line facing the ball. Ie:
int paddleY1 = paddle.y;
int paddleY2 = paddle.y + paddle.width;
// assuming the paddle can only go up and down, y is the only co-ordinate that matters.
Then, you can compute the mid point of the paddle as:
int paddleYMid = (paddleY1 + paddleY2) / 2;
You can find out if the ball hit the left or right side of the paddle by comparing the y co-ordinates. Ie:
if(ball.y > paddleYMid)
{
// Right side of the paddle.
}
else
{
// Left side of the paddle.
}
Then it's up to you to develop further refinement.
Since the paddle is always vertical (parallel to Y axis), the point of collision of the ball and the paddle will happen at the same y-coordinate as the center of the ball. That is:
if (ball.centerX - ball.radius <= paddle.rightX && ball.velocityX < 0)
{
// collision point, if any, is at (ball.centerX - ball.radius, ball.centerY)
if (ball.centerY >= paddle.bottomY && ball.centerY <= paddle.topY)
{
// handle collision
}
}
As for the handling of the collision itself, you may not have to deal with angle of reflection, etc, but work solely with the raw values of x and y velocity. For example, to simulate a perfectly elastic collision, simply do:
ball.velocityX = -ball.velocityX;
And to account for ball reflecting at a higher or lower angle, you can scale the y velocity based on the position of the collision from the center of the paddle, eg.
ball.velocityY += SCALE_CONSTANT * (ball.centerY - ((paddle.topY + paddle.bottomY) / 2));
To find the exact spot where the ball hits the paddle, you can formulate the problem as a line intersection problem.
The paddle can be represented as a vertical line at the x-coordinate (+thickness if needed, and corrected for the balls diameter) of the paddle. The ball can then be represented as a line along its movement vector (this line could be simply defined by its current position and its next position if it were to move unimpended).
The problem can now be solved using a line intersection algorythm.
Since the paddle is a vertical line, the equations can be simplified to just answer the question at which Y will the ball pass the paddle's X. Thats also a common problem encountered and solved by line clipping: http://en.wikipedia.org/wiki/Line_clipping (the intersection points can also be computed directly, but I can't find the formula atm).
I'm working on some artificial intelligence, and I want to be able for my AI not to run into given coordinates as these are references of a wall/boundary.
To begin with, every time my AI hits a wall, it makes a reference to that position (x, y). When it hits the same wall three times, it uses linear check points to 'imagine' there is a wall going through these coordinates.
I want to now prevent my AI from going into that wall again.
To detect if my coordinates make a straight line, I use:
private boolean collinear(double x1, double y1, double x2, double y2, double x3, double y3) {
return (y1 - y2) * (x1 - x3) == (y1 - y3) * (x1 - x2);
}
This returns true is the given points are linear to one another.
So my problems are:
How do I determine whether my robot is approaching the wall from its current trajectory?
Instead of Java 'imagining' there's a line from 1, to 3. But to 'imagine' a line all the way through these linear coordinantes, until infinity (or close).
I have a feeling this is going to require some confusing trigonometry?
For #2, you could check if the slope between any point and one point on the wall/line you want, is the same as the slope between two points on the line.
private boolean onWall(double x, double y, double wallX1, double wallY1, double wallX2, double wallY2) {
return (wallY1 - y) / (wallX1 - x) == (wallY2 - wallY1) / (wallX2 / wallX1);
}
So, the slopes calculated share a point, so if they're the same, they're all on the same line.
Interesting problem. Well two appraoches come to my mind:
So what you can do is that, once every line is detected, store its slope m and line constatnt c as per line y= mx +c. So once you change your co-ordinate to a new co-ordinate. put your new (x1,y1) in above line equation to see if y1 == m*x1 + c. The whole operation will be computationally expensive as O(n) where n is number of lines detected for every new co-ordinate movement
You can reduce the above by clustering points and checking the line matching as per cluster rather than for every line. i.e. store what all lines pass through a cluster and check only for those lines when you are currently in the respective cluster. This should be an ideal solution
Another solution would be to have an imaginary circle of radius r around your current point. Once the circle is obtained, find what all lines pass through the current cluster (as per explained above). For every new movement, check wall or not only for those lines. Once you move out of your cluster, draw a new circle again
I think this problem is more suitable for programmers.stackexchange.com rather than here :)