473,757 Members | 8,085 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(Re ctangle r1, Rectangle r2)

this should return the intersection of the 2 rectangles
anyone can help me plz?
Nov 14 '05 #1
6 17581
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(Re ctangle 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.hel sinki.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.goog le.com...
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(Re ctangle r1, Rectangle r2)

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


Rectangle intersection(Re ctangle 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.ne t> <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*****@tang o-sierra-oscar-foxtrot-tango.fake.org> wrote in message
news:10******** *****@corp.supe rnews.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
9173
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 = "the_car_of_my_fried_is_bigger_as_mine_but_my_girlfriend_is_more_beautifull" string_2 = "my_girlfriend_is_more_beautifull_and_has_blue_eyes"
17
2784
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= >>> (l,round(m))]
3
1831
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 either O1 or O2 is a pair of not and a value (a namespace name or ·absent·) and the other is a set of (namespace names or ·absent·), then that set, minus the negated value if it was in the set, minus ·absent· if it was in the set, must be the value.
15
9076
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 the purpose of learning to work with C++. It therefore makes some sense for me to give the situation the amount of consideration presented below. To be quite honest, I'm amazed at the amount there is to say about such a seemingly simple...
4
3371
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 variying by few characters) to the other column as follows: QUERY1 QUERY2
3
22461
by: cai_rongxi | last post by:
Hi, Can some body share the code to find the intersection of two rectangles? Thanks in advance
3
2462
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 object. Rectangle myRec = new Rectangle(0, 0, 2, 2); If you draw this rectangle on the screen you will end up with a rectangle like the one shown below (The character "X" represents a pixel used to draw the rectangle and the character "O" is...
2
2318
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: s1.intersection(s2) I want the item matching to be a bit "looser".. that is, items in s2 that match to just the beginning of items in s1 would be included in the result of intersection().
11
8564
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 - we're going to do intersections later anyway l1 = reduce(operator.add, list(x) for x in l1) l2 = reduce(operator.add, list(x) for x in l2) l3 = reduce(operator.add, list(x) for x in l3)
0
9489
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9298
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10072
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9906
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9885
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8737
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6562
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3829
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3399
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.