12,756,705 members (30,288 online)
Rate this:
See more: , +
hi friends ,please help me, i have many rectangles attached with each other on google map, i want to check a point(24.5271348225978,39.5751950517297) is lies inside the all the rectangles or not,
My rectangles latitude and longitude are:
```[
((24.571846433888872, 38.904972188566475), (25.006041783055572, 39.06448645904431)),
((24.427114650833303, 39.06448645904431), (25.58496891527784, 39.22400072952214)),
((24.137651084722165, 39.22400072952214), (25.58496891527784, 39.54302927047783)),
((24.137651084722165, 39.54302927047783), (24.861310000000003, 39.702543540955666)),
((24.282382867777734, 39.702543540955666), (24.716578216944437, 39.86205781143352))
] ```

Code block added, Urgency deleted - OriginalGriff[/edit]
Posted 26-Jul-12 0:07am
Updated 7-Aug-12 20:29pm
v3
OriginalGriff 26-Jul-12 5:14am

Urgency deleted: It may be urgent to you, but it isn't to us. All that your stressing the urgency does is to make us think you have left it too late, and want us to do it for you. This annoys some people, and can slow a response.

Rate this:

## Solution 1

Shouldn't this be done using a loop to go from the first one to the last one checking if the point is inside each rectangle?

Given that your point to be checked is Xp,Yp and you have n rectangles defined by 2 points in a plane (XY): Xr1[n],Yr1[n], Xr2[n],Yr2[n], shouldn't it be as easy as something like:

This is pseudocode to achieve that... probably it is too simple and I've not understood properly your question...
```n=1;
bInsideAllOfthem = true;
while bInsideAllOfthem and n <= number_of_rectangles
do
bInsideAllOfthem = ((((Xr1[n]-Xp > 0) and (Xr2[n]-Xp < 0)) or ((Xr1[n]-Xp < 0) and (Xr2[n]-Xp > 0))) and
(((Yr1[n]-Yp > 0) and (Yr2[n]-Yp < 0)) or ((Yr1[n]-Yp < 0) and (Yr2[n]-Yp > 0))));
n++;
end_while```

Hope this helps...
v4
sjelen 26-Jul-12 6:56am

Doesn't this work only if rectangles are aligned to X-Y axes?
Arbitrary rectangle in a plane can not be defined with only 2 points.
Joan Murt 26-Jul-12 7:06am

Given the fact Pardeep is speaking about latiutude and longitude which are perfectly aligned to the XY axes... and that the only values that Pardeep has shown are 2 pairs for each rectangle... I guess the code will work, taking into account what you point (which would be correct) then we would have to rotate the rectangle in the space and make the check like in my sample, do it using vectors or any other method we could think about...

hi joan murt thanks for reply but this not work correctly
Joan Murt 30-Jul-12 4:54am

why?

hi joan murt i can't understand the what value i have to pass into these variable Xr1[n],Yr1[n], Xr2[n],Yr2[n], Please can you explain it bit in details , sorry i have used your code but, i think , i have not use this in correct manner please , explain it, thanks in advance
Joan Murt 6-Aug-12 2:32am

Xr1[n],Yr1[n]: x and y coordinates of one of the given rectangles coordinates.
Xr2[n],Yr2[n] the other x and y coordinates that define the rectangle.

I'm curious: you told me that this would not work, and now you tell me you have used this code even you don't understand it. Why of both?

private bool chek()
{
double[] Xr1 = new double[500];
double[] Xr2 = new double[500];
double[] Yr1 = new double[500];
double[] Yr2 = new double[500];

string ss = "((29.80676507727775, 77.78045666901204), (29.997811030911105, 78.0013366676048)),((29.80676507727775, 78.0013366676048), (30.06149301545555, 78.0749633338024)),((29.80676507727775, 78.0749633338024), (30.125175000000002, 78.14859000000001)),((29.80676507727775, 78.14859000000001), (30.252538969088896, 78.22221666619758)),((29.8704470618222, 78.22221666619758), (30.443584922722252, 78.29584333239518)),((29.93412904636666, 78.29584333239518), (30.443584922722252, 78.36946999859278)),((29.997811030911105, 78.36946999859278), (30.443584922722252, 78.44309666479036)),((30.18885698454445, 78.44309666479036), (30.443584922722252, 78.51672333098796))".Replace("((", "").Replace(")),", "~");
string value = ss .Replace("((", "").Replace(")),", "~");

string ss2 = value.Replace("),", ":").Replace("))", "").Replace("(", "");
string[] ss1 = ss2.Split('~');

for (int i = 0; i < ss1.Count(); i++)
{
string str = ss1[i];
string[] str1 = str.Split(':');
string sstr2 = str1[0];
string sstr3 = str1[1];

string[] ssr = sstr2.Split(',');
string[] ssr2 = sstr3.Split(',');
Xr1[i] = Convert.ToDouble(ssr[0]);
Xr2[i] = Convert.ToDouble(ssr2[0]);
Yr1[i] = Convert.ToDouble(ssr[1]);
Yr2[i] = Convert.ToDouble(ssr2[1]);

}

//Property Points which are not show inside this rectangles
double Xp = 30.4676141022579;
double Yp = 77.1487426757813;

//Points Which are show inside the rectangles
//double Xp =30.3911639988066
//double Yp =76.7914295196533

bool bInsideAllOfthem = true;
for (int n = 0; n < ss1.Count(); n++)
{

bInsideAllOfthem = ((((Xr1[n] - Xp > 0) && (Xr2[n] - Xp < 0)) || ((Xr1[n] - Xp < 0) && (Xr2[n] - Xp > 0))) ||

(((Yr1[n] - Yp > 0) && (Yr2[n] - Yp < 0)) || ((Yr1[n] - Yp < 0) && (Yr2[n] - Yp > 0))));

}
return bInsideAllOfthem;
}

hi Joan Murt, firstly i want to tell you in detail that , i have street(startPoint=30.37761431777479, 78.40667724609375,EndPoint=29.871610558533725,77.89031982421875)
I want show the point along with the around the 10miles area of street, which i have given in points in the form of rectangles, and a two property in which one is near to street and than another, but when i have used your code , for check the point inside the area, the show smaller distance property from the street outside the area and larger distance property outside the area, i have helpless to finding the error, please help me, for resolving this error, thanks in advance and sorry for not clearing the issue.
Joan Murt 6-Aug-12 9:44am

First of all, you should improve your question with new details. This gives you the opportunity to get other members interest in your question and to put your code inside pre tags (which make it look like code).
Apart of that the inmediate problem I can see here is that you've used a for loop, which is not what I used in my sample. this gives true or false depending on the last set of values.

Given the fact that you want to know if your points are inside ALL your rectangles, you can stop searching at the point in which you find one rectangle that is not containing your points and/or you've reached the end. This is donde by a WHILE loop or with a FOR and a break statement (which I don't like).

Try it and come back with the result. ;)

hi joan , thanks for give me time, i have used the while loop , but this can't work please check my new code , but this work same wrong , they show near point outside the rectangles and larger distance point inside, please help,
private bool chek()
{
double[] Xr1 = new double[500];
double[] Xr2 = new double[500];
double[] Yr1 = new double[500];
double[] Yr2 = new double[500];
string ss = "((29.80676507727775, 77.78045666901204), (29.997811030911105, 78.0013366676048)),((29.80676507727775, 78.0013366676048), (30.06149301545555, 78.0749633338024)),((29.80676507727775, 78.0749633338024), (30.125175000000002, 78.14859000000001)),((29.80676507727775, 78.14859000000001), (30.252538969088896, 78.22221666619758)),((29.8704470618222, 78.22221666619758), (30.443584922722252, 78.29584333239518)),((29.93412904636666, 78.29584333239518), (30.443584922722252, 78.36946999859278)),((29.997811030911105, 78.36946999859278), (30.443584922722252, 78.44309666479036)),((30.18885698454445, 78.44309666479036), (30.443584922722252, 78.51672333098796))".Replace("((", "").Replace(")),", "~");
string value = ss.Replace("((", "").Replace(")),", "~");
string ss2 = value.Replace("),", ":").Replace("))", "").Replace("(", "");
string[] ss1 = ss2.Split('~');
for (int i = 0; i < ss1.Count(); i++)
{
string str = ss1[i];
string[] str1 = str.Split(':');
string sstr2 = str1[0]; string sstr3 = str1[1];
string[] ssr = sstr2.Split(','); string[] ssr2 = sstr3.Split(',');
Xr1[i] = Convert.ToDouble(ssr[0]); Xr2[i] = Convert.ToDouble(ssr2[0]);
Yr1[i] = Convert.ToDouble(ssr[1]); Yr2[i] = Convert.ToDouble(ssr2[1]);
} //Property Points which are not show inside this rectangles
//double Xp = 30.4676141022579; double Yp = 77.1487426757813;
//Points Which are show inside the rectangles
double Xp =30.3911639988066;
double Yp = 76.7914295196533;
int n = 0;

bool bInsideAllOfthem = false;
while (bInsideAllOfthem == false && n <= ss1.Count())
{
bInsideAllOfthem = ((((Xr1[n] - Xp > 0) && (Xr2[n] - Xp < 0)) || ((Xr1[n] - Xp < 0) && (Xr2[n] - Xp > 0))) ||

(((Yr1[n] - Yp > 0) && (Yr2[n] - Yp < 0)) || ((Yr1[n] - Yp < 0) && (Yr2[n] - Yp > 0))));
n++;

}
return bInsideAllOfthem;
}
Joan Murt 8-Aug-12 1:39am

Take a look at the code I wrote...
The bool var must be true and you must be checking true and not false inside the loop.
Doing it like you've done makes the program to stop looping just after having found one that's inside. You need the oposite.
This is very basic. You should be using the debugger which would help you.

hi joan , i have cheked but when i use
this
bool bInsideAllOfthem = true;
while (n <= ss1.Count())
{
bInsideAllOfthem = ((((Xr1[n] - Xp > 0) && (Xr2[n] - Xp < 0)) || ((Xr1[n] - Xp < 0) && (Xr2[n] - Xp > 0))) ||

(((Yr1[n] - Yp > 0) && (Yr2[n] - Yp < 0)) || ((Yr1[n] - Yp < 0) && (Yr2[n] - Yp > 0))));
n++;
if(bInsideAllOfthem)
{
break;
}
}
this show me same, and i have to chek when the point inside the rectangles they return true and i break the loop,
Joan Murt 8-Aug-12 4:07am

Well...

The code you are using is not the same code than I posted the first day. You can't change things without checking they will work.

In the code I put In my first post I'm checking two conditions:

1. bInsideAllOfThem == TRUE

2. n<MAX

You are breaking the code not in the moment you find one that is not OK, you are exiting the loop just at the moment you find one that is OK. You are doing it wrong.

Use the debugger, you'll be able to see how the variables change at each step.

sorry joan , I'm not getting you what you want to say , please may you correct the my code , which i have posted, i have used debugger every time, but same result found may , tell me with improved my code condition which i have posted on that, thnx for reply me
Joan Murt 8-Aug-12 6:37am

bool bInsideAllOfthem = true;
while ((bInsideAllOfthem) && (n <= ss1.Count()))
{
bInsideAllOfthem = ((((Xr1[n] - Xp > 0) && (Xr2[n] - Xp < 0)) || ((Xr1[n] - Xp < 0) && (Xr2[n] - Xp > 0))) ||
(((Yr1[n] - Yp > 0) && (Yr2[n] - Yp < 0)) || ((Yr1[n] - Yp < 0) && (Yr2[n] - Yp > 0))));
n++;
}

/*
Here you can check the value of bInsideAllOfthem which will be true only if the point was inside all the rectangles and false if at least one of the rectangles is not containing the point.
Doing that you ensure that you will leave the loop just at the end if all the rectangles contain the given points or as soon as one of the checked rectangles is not containing the points.
*/

Your last code was not doing that... your code was breaking the loop at the first case in which a rectangle contained the point.

Thanks a lot Joan murt for help ,this works fine, i hope u will help me in future
Joan Murt 8-Aug-12 9:10am

Don't worry, I'll help if I can (as the other CPians here).
About the code take a look at the thread you've posted from the beginning till the end and understand the slight differences that are between the loop models you've proposed and the one that works. ;)
lokesh lokesh 26-Apr-13 2:46am

Hi this lokesh your post is very very very use full for me

thanks dudes
Rate this:

## Solution 2

Here is a good tip:
Is a Point inside a Polygon?[^]
It's meant to work in 2D plane, but it will serve the purpose if you don't need high precision.
The same algorithm can be applied to spherical geo-coordinates, but it includes a lot of trigonometry and is a lot slower.
http://en.wikipedia.org/wiki/Point_in_polygon[^]
P.S.: Here is a another good example:
Classes for computational geometry[^]
The `CPolygon` class has `PointIn` method.
v2

thanks for solution

sorry , but by this they pick the all the point placed from the right of reactangles

Top Experts
Last 24hrsThis month
 Graeme_Grant 714 Maciej Los 255 Jochen Arndt 231 OriginalGriff 188 ppolymorphe 131
 OriginalGriff 4,159 Peter Leow 3,179 ppolymorphe 2,491 Karthik Bangalore 2,470 Graeme_Grant 2,101

Web01 | 2.8.170217.1 | Last Updated 8 Aug 2012