473,387 Members | 1,569 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Rectangle intersection

hi all,

how to calculate the intersection of 2 rectangle

a rectangle is the following:

Rectangle makeRectangle (Point lowerLeft, Point upperRight) {

Rectangle r;

r.pt1 = lowerLeft;
r.pt2 = upperRight;
return r;
}

and Point is the following:

Point makePoint(int x, int y) {
Point p;
p.x = x;
p.y = y;
return p;
}
And i want the know how to make:

Rectangle intersection(Rectangle r1, Rectangle r2)

this should return the intersection of the 2 rectangles
anyone can help me plz?
Nov 14 '05 #1
6 17539
kimos <ur***@hotmail.com> scribbled the following:
hi all, how to calculate the intersection of 2 rectangle a rectangle is the following: Rectangle makeRectangle (Point lowerLeft, Point upperRight) {

Rectangle r; r.pt1 = lowerLeft;
r.pt2 = upperRight;
return r;
} and Point is the following: Point makePoint(int x, int y) {
Point p;
p.x = x;
p.y = y;
return p;
}
And i want the know how to make: Rectangle intersection(Rectangle r1, Rectangle r2) this should return the intersection of the 2 rectangles
anyone can help me plz?


Which part are you having trouble with, calculating the intersection's
coordinates or implementing the algorithm as a C program?

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Immanuel Kant but Genghis Khan."
- The Official Graffitist's Handbook
Nov 14 '05 #2

"kimos" <ur***@hotmail.com> wrote in message
news:ae*************************@posting.google.co m...
hi all,

how to calculate the intersection of 2 rectangle

a rectangle is the following:

Rectangle makeRectangle (Point lowerLeft, Point upperRight) {

Rectangle r;

r.pt1 = lowerLeft;
r.pt2 = upperRight;
return r;
}

and Point is the following:

Point makePoint(int x, int y) {
Point p;
p.x = x;
p.y = y;
return p;
}
And i want the know how to make:

Rectangle intersection(Rectangle r1, Rectangle r2)

this should return the intersection of the 2 rectangles
anyone can help me plz?


Rectangle intersection(Rectangle r1, Rectangle r2)
{
Find the leftmost and the bottommost rectangle.
Find the rightmost and the topmost rectangle.

If the left x co-ordinate of the non-leftmost rectangle is between the
left and the right of the leftmost rectangle, you have the left cordinate of
your intersection. If it is to the right you have no intersection, and the
rectangles don't overlap. (If it is to the left you didn't calculate the
leftmost rectangle correctly).

Repeat for all the other coordinates (bottom, right, top).
}
Nov 14 '05 #3
kimos <ur***@hotmail.com> wrote:
hi all,

how to calculate the intersection of 2 rectangle


Take the rightmost left-border, the left-most right border, the bottom-most
top border and the top-most bottom border as borders for your intersection.
If the left border of the intersection is on the right of the right border
ot the top border below the bottom border, there is no intersection.
--
Simon Stienen <http://dangerouscat.net> <http://slashlife.de>
»What you do in this world is a matter of no consequence,
The question is, what can you make people believe that you have done.«
-- Sherlock Holmes in "A Study in Scarlet" by Sir Arthur Conan Doyle
Nov 14 '05 #4
# this should return the intersection of the 2 rectangles
# anyone can help me plz?

Take two pieces of paper, overlay them, and think about you're looking at. Hint:
the intersection is either empty, a point, or a rectangle.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
OOOOOOOOOO! NAVY SEALS!
Nov 14 '05 #5

"SM Ryan" <wy*****@tango-sierra-oscar-foxtrot-tango.fake.org> wrote in message
news:10*************@corp.supernews.com...
# this should return the intersection of the 2 rectangles
# anyone can help me plz?

Take two pieces of paper, overlay them, and think about you're looking at. Hint: the intersection is either empty, a point, or a rectangle.


Or two points forming a line segment (the
rectangles share some part of a border).
Nov 14 '05 #6
kimos wrote:
how to calculate the intersection of 2 rectangle


The easiest way to think about this problem conceptually is to first
come up with an algorithm for the 1D version, intersecting two line
segments, then generalize it.

Suppose we have a line from a to b and a line from c to d. There are
four cases for intersecting them:

a <= b < c <= d : a----b c----d
In this case the intersection is empty.

a <= b = c <= d : a----b/c----d
In this case the intersection is the single point b=c (the line segment
from b to b.)

a <= c <= b <= d : a---c===b---d
In this case the intersection is the line from c to b.

a <= c <= d <= b : a---c===d---b
In this case the intersection is the line from c to d.

If you simply apply these same four cases to the x and y coordinate
ranges of your rectangles separately, the result will be the x and y
coordinate ranges of the intersection of the rectangles.
--
Derrick Coetzee
I grant this newsgroup posting into the public domain. I disclaim all
express or implied warranty and all liability. I am not a professional.
Nov 14 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Antoine Logean | last post by:
Hi, What is the easiest way to get the intersection of two strings in python (a kind a "and" operator) ? ex: string_1 =...
17
by: Gordon Williams | last post by:
Hi, I have to lists that I need to find the common numbers (2nd rounded to nearest integral) and I am wondering if there is a more efficient way of doing it. >>> a= >>> b= >>> ...
3
by: Alain Frisch | last post by:
Hello, The following rule in the XML Schema spec, section "Schema Component Constraint: Attribute Wildcard Intersection" seems strange to me: ======================================= 3 If...
15
by: Steven T. Hatton | last post by:
The following may strike many of you as just plain silly, but it represents the kind of delelima I find myself in when trying to make a design decision. This really is a toy project written for...
4
by: Sebastian Cohen S | last post by:
Hello, I am new to SQL and currently using Access 2003 and need a little help on the following. I have two queries each contains one column formed with a string that is very similar (only...
3
by: cai_rongxi | last post by:
Hi, Can some body share the code to find the intersection of two rectangles? Thanks in advance
3
by: | last post by:
I am having a hard time understanding the logic behind the Rectangle object. My problem has to do with the way the rectangle treats the "Width" property. For example, take the following rectangle...
2
by: mkppk | last post by:
I have kind of strange change I'd like to make to the sets.Set() intersection() method.. Normally, intersection would return items in both s1 and s2 like with something like this: ...
11
by: Prateek | last post by:
I have 3 variable length lists of sets. I need to find the common elements in each list (across sets) really really quickly. Here is some sample code: # Doesn't make sense to union the sets -...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.