There is a problem:
Have a rectangle, the coordinates of the lower left corner - (0, 0), upper right (width of the rectangle, the height of the rectangle). Inside this rectangle is the point having certain coordinates. I need to draw around this point a segment of a circle with a certain (const) arc length. Calculate the internal radius and the angle of the segment of a circle.
illustration:
Like this. As can be seen from the figure, if the segment to be placed in the side of the screen (eg bottom left), it is necessary to find such a range that it satisfies our long arc.
Prompt, in what side to search?
Thank you in advance.
Related
I want to draw a curved path (an arc or a cubic bezier curve) from a point to a circle. The following parameters are known: pX (x-position of the point), pY (y-position of the point), cX (x-position of the circle's center), cY (y-position of the circle's center) and cR (radius of the circle).
This figure illustrates the problem.
The circle is not opaque and when the path is drawn from the point to the circle's center, it is visible through the circle (see #1 in the linked figure). It shouldn't be happening as there exists some background content that has to be visible through the circle. So, the solution I imagine is to draw the path up to the circle's border.
Drawing a straight line until the circle's border is easy (see #2 in the linked figure):
var theta = Math.atan2(cY - pY, cX - pX);
var startX = pX;
var startY = pY;
var endX = cX - cR * Math.cos(theta);
var endY = cY - cR * Math.sin(theta);
line.attr('x1', startX).attr('y1', startY).attr('x2', endX).attr('y2', endY);
If I apply the same principle to a curved path, it will be drawn incorrectly (see #3 in the linked figure). It has to keep the shape as if it was pointing to the circle's center, but end without overlapping the circle (see #4 in the linked figure).
Additional observations:
the curve can be as thick as the circle
there can exist several hundred of circle-arc pairs simultaneously in the visualization, therefore the performance has also be taken into consideration
Any suggestions on how to solve this? Thanks in advance!
Look at this picture:
The red dot is your point (pX,pY), the black dot is the center of the circle. You choose the green point somewhere in the plane (you can experiment with its location to find a curve that you like) and construct the blue point as the intersection of your circle with the straight line connecting the black and green points.
Now you use the red point, the green point and the blue point as a control polygon to construct a quadratic Bézier curve. It will start from the red point, end in the blue point on the circle and its tangent there will be towards the center of the circle.
If your curve would be really thick, this simple solution might be insufficient. In that case I recommend doing as #rioV8 recommends.
I want to rotate a rectangle in a coordinate system. I already know how to rotate each point in it around the 0,0 position, but what I want is to rotate the rectangle around it's minimum x,y point (bottom left, so to speak). I already tried "normalizing" the points first (to 0,0) and then adding the offset again, but my math seems to be faulty.
Two images to clarify. The red circle is the point around which the points in the retangle are rotated. The min (=minimum) point around which to rotate is at 2,1 (bottom left of the dark green rectangle)
First image: What I already can do.
Second image: What I want to accomplish
I have tried and failed to find an Applescript code that returns a smart object's current rotation angle in Photoshop. Anyone have an idea of where that property is listed? I'm beginning to think this feature isn't currently supported by Applescript.
In Photoshop, objects like a selection has no angle value, because it means nothing: if your selection is made by multiple segments making a complexe shape, there is no mathematical way you can define angle for that shape !
However, you can work with boundary rectangle (which includes that shape). You can rotate this complete boundary (i.e. the selection) and then you will get a new boundary (new rectangle where new rotated shape fits in).
A boundary rectangle is made of a list of for values :
top left corner horizontal position (X1)
top left corner vertical position (Y1)
bottom right corner horizontal position (X2)
bottom right corner vertical position (Y2)
Positions are real numbers, starting with border of canvas (not border of layer ! so you may have negative values). The units depends of the unit of measure of the document.
Once that's clear (I hope !) if you use mathematical calculation between initial boundary and new boundary, you can calculate the rotation angle:
(Pythagore triangle)
If you assume that initial rectangle borders were vertical and horizontal :
cosinus (Teta) = (X2-X1) / (X'2 - X'1)
Teta = angle you are looking for
X1, X2 are the positions of the boundary corners before rotation and X'1, X'2 are position of same corners after rotation.
Please note that this method is OK for selection (any shape), or layers.
It should also be OK for the full canvas, but I never test it for canvas.
In a 2d space there are a rectangle and a circle that overlap each other. How can
I calculate the smallest distance (depth) that I need to separate the circle and the rectangle?
I'll assume from the way you've described it if one shape entirely contains the other, that still counts as "overlapping"
The strategy to separate a circle from a rectangle while moving the circle the shortest distance is as follows:
Draw a line from the circle's centre to the nearest point on one of the rectangle's vertices
Pull the circle along this line until they are no longer overlapping
So to calculate the distance that it needs to be pulled, your formula will be:
pullDistance = radius - centreDistance
Where:
pullDistance is what you're trying to calculate
radius is the radius of the circle
centreDistance is the distance of the centre of the circle from the nearest point on the edge of the rectangle.
Two things to note:
If the centre of the circle is inside the rectangle, then centreDistance should be calculated the same way, but made negative
If the pullDistance is negative then the two shapes are already not overlapping, so the true distance is 0.
So since radius is known, all you have to do is calculate the centreDistance. The way to do this is to find the distance from the circle's centre point to each of the rectangle's four line segments and take the minimum. Finding the distance between a point and a line segment is a common task, I won't repeat how to do that here. This question has a lot of samples and information for how to do it.
I've just implemented collision detection using SAT and this article as reference to my implementation. The detection is working as expected but I need to know where both rectangles are colliding.
I need to find the center of the intersection, the black point on the image above (but I don't have the intersection area neither). I've found some articles about this but they all involve avoiding the overlap or some kind of velocity, I don't need this.
The information I've about the rectangles are the four points that represents them, the upper right, upper left, lower right and lower left coordinates. I'm trying to find an algorithm that can give me the intersection of these points.
I just need to put a image on top of it. Like two cars crashed so I put an image on top of the collision center. Any ideas?
There is another way of doing this: finding the center of mass of the collision area by sampling points.
Create the following function:
bool IsPointInsideRectangle(Rectangle r, Point p);
Define a search rectangle as:
TopLeft = (MIN(x), MAX(y))
TopRight = (MAX(x), MAX(y))
LowerLeft = (MIN(x), MIN(y))
LowerRight = (MAX(x), MIN(y))
Where x and y are the coordinates of both rectangles.
You will now define a step for dividing the search area like a mesh. I suggest you use AVG(W,H)/2 where W and H are the width and height of the search area.
Then, you iterate on the mesh points finding for each one if it is inside the collition area:
IsPointInsideRectangle(rectangle1, point) AND IsPointInsideRectangle(rectangle2, point)
Define:
Xi : the ith partition of the mesh in X axis.
CXi: the count of mesh points that are inside the collision area for Xi.
Then:
And you can do the same thing with Y off course. Here is an ilustrative example of this approach:
You need to do the intersection of the boundaries of the boxes using the line to line intersection equation/algorithm.
http://en.wikipedia.org/wiki/Line-line_intersection
Once you have the points that cross you might be ok with the average of those points or the center given a particular direction possibly. The middle is a little vague in the question.
Edit: also in addition to this you need to work out if any of the corners of either of the two rectangles are inside the other (this should be easy enough to work out, even from the intersections). This should be added in with the intersections when calculating the "average" center point.
This one's tricky because irregular polygons have no defined center. Since your polygons are (in the case of rectangles) guaranteed to be convex, you can probably find the corners of the polygon that comprises the collision (which can include corners of the original shapes or intersections of the edges) and average them to get ... something. It will probably be vaguely close to where you would expect the "center" to be, and for regular polygons it would probably match exactly, but whether it would mean anything mathematically is a bit of a different story.
I've been fiddling mathematically and come up with the following, which solves the smoothness problem when points appear and disappear (as can happen when the movement of a hitbox causes a rectangle to become a triangle or vice versa). Without this bit of extra, adding and removing corners will cause the centroid to jump.
Here, take this fooplot.
The plot illustrates 2 rectangles, R and B (for Red and Blue). The intersection sweeps out an area G (for Green). The Unweighted and Weighted Centers (both Purple) are calculated via the following methods:
(0.225, -0.45): Average of corners of G
(0.2077, -0.473): Average of weighted corners of G
A weighted corner of a polygon is defined as the coordinates of the corner, weighted by the sin of the angle of the corner.
This polygon has two 90 degree angles, one 59.03 degree angle, and one 120.96 degree angle. (Both of the non-right angles have the same sine, sin(Ɵ) = 0.8574929...
The coordinates of the weighted center are thus:
( (sin(Ɵ) * (0.3 + 0.6) + 1 - 1) / (2 + 2 * sin(Ɵ)), // x
(sin(Ɵ) * (1.3 - 1.6) + 0 - 1.5) / (2 + 2 * sin(Ɵ)) ) // y
= (0.2077, -0.473)
With the provided example, the difference isn't very noticeable, but if the 4gon were much closer to a 3gon, there would be a significant deviation.
If you don't need to know the actual coordinates of the region, you could make two CALayers whose frames are the rectangles, and use one to mask the other. Then, if you set an image in the one being masked, it will only show up in the area where they overlap.