objective c - Find mid-point between two rectangles, intersection + edge aware -
as seen in image, find mid-point between 2 rectangles. if rectangles intersecting mid-point between centers of rectangles. if rectangles not intersecting, mid-point from/between edges of rectangles.
i write in obj-c or swift.
thx
in situation should check first check wether there rectangles overlapped or not.
to check wether overlapped or not
cgrect recta = cgrectmake(50, 50, 50, 50);
cgrect rectb = cgrectmake(100, 100, 50, 50);
if (cgrectgetminx(recta) < cgrectgetmaxx(rectb) && cgrectgetmaxx(recta) > cgrectgetminx(rectb) && cgrectgetminy(recta) < cgrectgetmaxy(rectb) && cgrectgetmaxy(recta) > cgrectgetminy(rectb) ) { nslog(@"overlapped"); } else { nslog(@"not overlapped"); }
after finding overlapped or not find centres of both rectangles , whatever want.
to find center.
cgpoint centerb = cgpointmake((cgrectgetminx(rectb) + cgrectgetmaxx(rectb))/2, (cgrectgetminy(rectb) + cgrectgetmaxy(rectb))/2);
cgpoint centera = cgpointmake((cgrectgetminx(recta) + cgrectgetmaxx(recta))/2, (cgrectgetminy(recta) + cgrectgetmaxy(recta))/2);
Comments
Post a Comment