Equivalent to CGRectIntersectsRect - ios

Is there an equivalent to CGRectIntersectsRect that would be something more along the lines of points intersecting... I'm making a game and RectIntersectsRect works for what I want it to do but it looks bad because sometimes the corner of the one object will intersect the corner of the other and it will call the method, and you can barely see that the two objects touched, so it looks like it just glitches. Is there a CGPointIntersectsPoint or something along the lines of that? Thanks.

You can use CGRectContainsPoint to see if one of your rectangle contains a corner of the other, but you will get the same issue.
You can look for the intersection of the two rectangles using CGRectIntersection. It will give you an other CGRect, which represents the area in common from your two rectangles. From this rectangle, you can check width and height to see if the intersection is "big enough"
CGRect intersection = CGRectIntersection(rect1, rect2);
if (CGRectGetWidth(intersection) > kHorizontalThreshold || CGRectGetHeight (intersection) > kVerticalThreshold)
{
// call your intersection method here
}

Try CGRectContainsPoint. If you want to just compare two points use CGPointEqualToPoint.
You can't really have a point intersecting another point as they're two exact points in space. The only way they can "intersect" is by being equal.

Related

How to tell if the lowest point in the output of BoxPoints(rect) is the bottom right or bottom left point of the bounding box?

In answer to another question, Sushanth stated:
The lowest point of the rectangle(does not matter left or right) will always be the first sub-list of the "box" ndarray. So in the example I have given, the first sub-list [169 144] represents the "bottom right of this rectangle".
Now this point will be the reference point to decide what the next sub-list represents. Meaning, the next sub-list will always represent the point that you first get when you move in the clockwise direction. (as shown in the second image of the for loop)
I don't understand how to tell if the lowest point is the bottom-left or bottom-right point on the bases of the "first sub-list".
I need to create a generalized code that can tell them apart so that I can reliably apply warpAffine transformation to a dataset of images (as shown here).
This is Sushanth's answer to my question:
To determine whether it is a bottom-left or right in a scenario when
the two bottom points have the same y-coordinate: i. First, have a
conditional statement to see whether the two bottom points have the
same y-coordinate. ii. If the condition is True, then check which
coordinate has the lowest x-value(or the highest). The coordinate
with the lowest x-value will be bottom-left of course! "How did you
determine this just by looking at the sub-list?" -- I did not
determine it by just looking at it! I could not! This is exactly why I
wrote the above for-loop in my answer!
with minor spelling corrections from my side
I learned later on that I actually didn't need that information for performing warpAffine transformation as the angle information is returned by minAreaRect
def warp_contour(img,cnt):
rows, cols = img.shape
rect = cv2.minAreaRect(cnt)
center,_,angle = rect
box = cv2.boxPoints(rect)
box = np.int0(box)
rot = cv2.getRotationMatrix2D(center, angle, 1)
img = cv2.warpAffine(img, rot, (rows,cols))
return img
But I won't be accepting this answer as I still want to know how to tell them apart.

PaintCode - move object on the path

I would like draw a curved line and attach an object to it. Is it possible to create fraction (from 0.0 to 1.0) which makes move my object on the path? When fraction is 0 then object is on the beginning, when 0.5 is on half way and finally when is on 1.0 it is at the end. Of course i want a curved path, not a straight line :) Is it possible to do in PaintCode?
If you need it only as a progress bar, it is possible in PaintCode. The trick is to use dashed stroke with very large Gap and then just change the Dash.
Then just attach a Variable and you are done.
Edit: Regarding the discussion under the original post, this solution uses points as the unit, so it will be distributed equally along the curve, no matter how curved the bezier is.
Based on the fact that you're going to walk along the curve using linear distance, a thing Bezier curves are terrible for, you need to build the linear mapping yourself. That's fairly simple though:
When you draw the curve, also build a look-up table that samples the curve once, at say 100 points (t=0, t=0.01, t=0.02, etc). In pseudocode:
lut = [];
lut[0] = 0;
tlen = curve.length();
for(v=0; v<=100; v++) {
t = v/100;
clen = curve.split(0,t).length();
percent = 100*clen/tlen;
lut[percent] = t;
}
This may leave gaps in your LUT - you can either fix those as a secondary step, or just leave them in and do a binary scan on your array to find the nearest "does have a value" percentage.
Then, when you need to show your progress as some percentage value, you just look up the corresponding t value: say you need to show 83%, you look up lut[83] and draw your object at the value that gives you.

How to draw infinitely long line in SCENEKIT?

I want to draw axes as that in SketchUp which are infinitely long. I already drew lines that are of fixed length but I want it infinitely long.
Fake it with fixed length lines that extend from beyond the camera's view (called the frustum), through it, and then beyond it again.
You can create beginning and ending SCNNode instances for each axis. Then every time the camera changes its view, call
func isNodeInsideFrustum(_ node: SCNNode,
withPointOfView pointOfView: SCNNode) -> Bool
on each of your 6 endpoints. If an endpoint is within the frustum, move it farther out until it's not.
Methods in the SCNSceneRendererDelegate protocol might be helpful to you.
Depending on the context of what you mean, you check for when the size of the line is about to cross some x or y axis in the screen. You can detect the size of the screen by using size.frame. Alternatively, you can use CGRectGetMidX, CGRectGetMidY, etc.
For example, I would do something like this:
var x = CGRectGetMaxX(self.frame)
//Where y is the max size of the line you have
if x <= y {
//Code where you make the line shape extend or add another line over it to make it look continous
}

Check if coordinate rectangle contains CLLocationCoordinate2D

I am using a special Map SDK for iOS and I am adding a custom shape to the map. The shape is always a different size and it could be a circle, square, star etc. the point being it is always dynamic whenever the app is run.
After adding this shape to the map, I can access it's property called overlayBounds which is described as: This property contains the smallest rectangle that completely encompasses the overlay.
The overlay is my shape that I'm adding to the map.
Whenever a location update is generated by CLLocationManager, I want to check and see if the most recent coordinate is inside of that overlayBounds property of the shape.
When accessing overlayBounds, it has an ne property and a sw property. Both of these are just CLLocationCoordinate2D's
So, if the overlayBounds is made up of two CLLocationCoordinate2D's and the CLLocationManager is always updating the user's location and giving me the most recent coordinate(CLLocationCoordinate2D), how can I check if that most recent coordinate is within the overlayBounds?
After doing a lot of research I have only found one potential solution to go off of which is this: https://stackoverflow.com/a/30434618/3344977
But that answer assumes that my overlayBounds property has 4 coordinates(CLLocationCoordinate2D's), when I only have 2.
Your description seems much harder then the actual question. So if I am getting this correctly your question is only to check if the point is inside the rectangle described in overlayBounds.
You have only 2 points as it is enough to define a rectangle. So NE and SW are the two points where the other two are received as (NE.x, SE.y) and (SE.x, NE.y). With this you may use the answer you linked or you may simply construct a MKMapRect where origin is NE and size is SE-NE. So in this case you may simply use MKMapRectMake and then use MKMapRectContainsPoint. BUT watch out when computing size as SE-NE might produce negative results in which cases you need to add degrees to the size. That is 180 to x (latitude) and 360 to y (longitude)...
MKMapRect rect = MKMapRectMake(NE.latitude, NE.longitude, SE.latitude-NE.latitude, SE.longitude-NE.longitude);
if(rect.width < .0) rect.width += 180.0;
if(rect.height < .0) rect.height += 360.0;
BOOL pointInside = MKMapRectContainsPoint(rect, pointOnMap);
Something like this should do the trick.
Now if you are trying to check if the point is inside the shape itself it really depends on how your shape is defined. If this is some form of analytic representation you might find some method already made for you to return the value but if not then your best shot would most likely be drawing the shape to some canvas and checking the color of canvas at the location you need to check. In any case the bigger problem here is converting the point and the rect to a Cartesian coordinate system. If that is the case then just add a comment and I will try to help you on that...

amount of rect intersection?

i am trying to check the degree of overlap between 2 CGPaths.
the easiest way i have come up with is get the percentage of the overlap between the bounding CGRects. I know this will fail when different paths occupy similar bounds. but oh well, if you know of a better way ... please help.
anyway, the current question regards calculating the percentage overlap between the rects.
i see the CGRectIntersection function to obtain the rectangle of intersection. I can calculate the area of this rect, but there doesn't seem to be an easy way to get the area of the non-intersected regions. any ideas? would subtracting that area from the area of the rectUnion make sense? if i understand rectUnion correctly, if the union and the intersection are the same size, they completely overlap?
Not quite understanding, I think. Isn't the "non-intersecting region" of a CGRect A with another one B just A's area minus the intersecting region? Or more to the point, isn't the percentage overlap just equal to the intersecting area divided by the combined total area:
Area(A ^ B)/(Area(A) + Area(B) - Area(A^B))
(BTW, I don't think you want to deal with RectUnion as that potentially has a huge amount of space in neither A or B. )
Oh, and on your original question, that's beyond my graphics ability, but the basic technique seems to be to draw both Paths in a graphic context (maybe with an XOR) and see which pixels are still left on. There seems to be some code pointing the way here: Clipping CGPath to a CGRect

Resources