Point in polygon

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gayathri81
    New Member
    • Jul 2012
    • 25

    #16
    Hello Sir

    I did as you told. First made as executable statements and wrote as
    ((A!=B)&&(C*D/E)); However I found that it does not enter the function at all in the main program DataProcessor.c c(I checked this by giving some statements like hello1, hello2 etc...). Does this mean my algorithm is wrong or there is some problem in calling function in main program? Please help me out.

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #17
      Have you set a breakpoint in your function to verify that it is not called? Your algorithm can't be wrong unless it is actually used to produce a result.

      Sometimes a comiler omits code that can never be reached:

      if (A is true) do this and return.
      if (A is not true) do this and return.
      Do some other processsing and return.

      The Do some other processing will never be reached so wile it may be in your code, the compile will generally drop it from the compiled code.

      Comment

      • gayathri81
        New Member
        • Jul 2012
        • 25

        #18
        I just modified the algorithm in Polygon.cc
        Code:
        bool Polygon::isInside(const Point& p)
         {
            int i,j= this->points.size()-1;
            bool oddnodes=false;
            for(i=0;i<this->points.size();i++)
            {
                if((this->points[i].longitude < p.longitude &&this->points[j].longitude>=p.longitude || this->points[j].longitude< p.longitude &&this->points[i].longitude>=p.longitude)&&
                        (this->points[i].latitude<=p.latitude|| this->points[j].latitude<=p.latitude))
                {
                    if(this->points[i].latitude+(p.longitude-this->points[i].longitude)/(this->points[j].longitude-this->points[i].longitude)*(this->points[j].latitude-this->points[i].latitude)<p.latitude)
                    {
                        oddnodes!=oddnodes;
                        
                    }
                }
                j=i;
            }
            return oddnodes;
        }
        However now I get an error "unable to resolve identifier points", though I have declared
        std::vector<Poi nt>points;

        Why is this hapening now? I never got this error before.
        Last edited by Banfa; Aug 14 '12, 12:36 PM. Reason: Added [code] [/code] tags

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #19
          Does polygon.cc #include polygon.h?

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #20
            weaknessforcats is away for a bit and has asked me to look after this thread.

            I have tried compiling your code (as given in this thread) and it compiles without the error you are getting which suggests that you may have failed to properly include the header for Polygon or some other file structural issue.

            Is this the only error you are getting? If not lets see them all please.

            What compiler are you using? Have you set it to produce all possible warnings.

            When I compile the above code I get these warnings

            Line 7 that it would be best to use parentheses () to make sure the logic of the if statement happens correctly.

            Line 12 the statement has no effect

            In oddnodes!=oddno des; != is the comparison operator that produces a boolean result, it does not change the value of its parameters. I believe that you where trying to toggle the value of oddnotes that would be oddnodes = !oddnodes;

            Comment

            • gayathri81
              New Member
              • Jul 2012
              • 25

              #21
              Yes. It is also included. But I have found out where the mistake is. Thanks a lot.

              Comment

              • gayathri81
                New Member
                • Jul 2012
                • 25

                #22
                I also found that I have to convert into degrees because of latitude and logitude. So I used this:
                Code:
                 double Dataprocessor::calculateDistance(const Point& a, const Point& b) 
                {
                	double lat = (a.latitude + b.latitude) / 2 * 0.01745;
                	double dLat = 111.3 * cos(lat) * (a.longitude -b.longitude);
                	double dLng = 111.3 * (a.latitude - b.latitude);
                    
                	return sqrt(pow(dLat, 2) + pow(dLng, 2));
                }
                Now I want to find out whether a point is inside the polygon or not. So I used the above function and the polygon.cc and wrote:
                Code:
                if(calculateDistance(position,.....))
                {
                // push back to geofence
                }
                In the above part, position is the position of test point and the other parameter would be the vertices of polygon. But it is not clear what should be written there? I want to compare the latitude and longitude of test point with each of the vertices of polygon using the ray casting algorithm.

                I wrote points but showed me an error , failing to recognise it. What should be written?
                Last edited by Banfa; Aug 16 '12, 12:04 PM. Reason: Added [code] [/code] tags

                Comment

                • Banfa
                  Recognized Expert Expert
                  • Feb 2006
                  • 9067

                  #23
                  points is a vector of Point, but calculateDistan ce is expecting a reference to a single Point.

                  You need to iterate through your vector of points and individually call the function for each one

                  Code:
                  for(std::vector<Point>::iterator iter = points.begin(); iter != points.end(); iter++)
                  {
                    double distance = calculateDistance(position, *iter);
                  
                    // Do something with distance
                  }

                  Comment

                  • gayathri81
                    New Member
                    • Jul 2012
                    • 25

                    #24
                    Hello
                    I perfectly agree with you. I have done what you have told. Now I want to call the polygon algorithm. I tried this out but did not work.
                    Code:
                    if(isInside(curPosition.position))
                    {
                    //push geofences inside.
                    }
                    // Function to check whether the point is inside or outside the 
                    //polygon : concept of ray casting algorithm
                    bool DataProcessor::isInside(Point& p)
                     {
                        
                        int i,j= this->points.size()-1;
                        bool oddnodes=false;
                        for(i=0;i<this->points.size();i++)
                        {
                            if((this->points[i].longitude < p.longitude && this->points[j].longitude >= p.longitude || this->points[j].longitude< p.longitude && this->points[i].longitude >= p.longitude)&&
                                    (this->points[i].latitude <= p.latitude|| this->points[j].latitude <= p.latitude))
                            {
                                if(this->points[i].latitude + (p.longitude-this->points[i].longitude) / (this->points[j].longitude-this->points[i].longitude) * (this->points[j].latitude-this->points[i].latitude) < p.latitude)
                                {
                                    oddnodes!=oddnodes;
                                    
                                }
                            }
                            j=i;
                        }
                           return oddnodes;
                    }
                    //All the code is in DataProcessor.c c.
                    //curPosition.pos ition is the current position of GPS device.

                    Where am I going wrong? There are no syntax errors. But I am no getting sms.
                    Last edited by Banfa; Aug 16 '12, 12:05 PM. Reason: Added [code] [/code] tags

                    Comment

                    • Banfa
                      Recognized Expert Expert
                      • Feb 2006
                      • 9067

                      #25
                      As I already pointed out in post #20 you have some errors that are causing your code to go wrong, for example oddnodes!=oddno des; does nothing.

                      Comment

                      • gayathri81
                        New Member
                        • Jul 2012
                        • 25

                        #26
                        Hallo

                        Yes you are right. This statement always returns me 0 even if the point is outside or inside. Or how should I modify the code above to get even for outside and odd for inside ( ray casting method)? Please help me out.

                        Comment

                        • Banfa
                          Recognized Expert Expert
                          • Feb 2006
                          • 9067

                          #27
                          As I already said I gave the solution to this in post #20

                          Comment

                          • gayathri81
                            New Member
                            • Jul 2012
                            • 25

                            #28
                            Hello

                            Thank you for the solution. But I could not understand this :
                            double distance = calculateDistan ce(position, *iter);

                            I am a starter with vector in C++ and I am trying to develop myself.

                            Comment

                            • Banfa
                              Recognized Expert Expert
                              • Feb 2006
                              • 9067

                              #29
                              This is just a call to a function
                              Code:
                              double distance = calculateDistance(position, *iter);
                              double distance = - we are declaring a variable to hold the result called distance and we will initialise it with something.

                              calculateDistan ce(position, *iter); the thing we are going to initialise it, the return value of the function calculateDistan ce, 2 parameters are being passed to this function, position (which came from your own post) and *iter. Sinceiter is an iterator into your vector by dereferencing iter (*iter) we get the value that the iterator is currently pointing at.

                              When using a standard container, such as std::vector iterators are used to address each entry in the container in turn, a little like using a pointer you access members of an array. Iterators can be incremented to access the next member and there are a variety of stand-library methods which can help, for example std::distance the distance between 2 iterators into the same container. All containers have methods begin and end to get the iterator to the first entry and the iterator just after the last entry and rbegin and rend which gets elements in reverse order.

                              Look at this example (run and compile and understand)

                              Code:
                              #include <iostream>
                              #include <vector>
                              #include <algorithm>
                              
                              
                              int main()
                              {
                              	// Declare my vector
                              	std::vector<int> numbers;
                              	
                              	// Populate it
                              	for(int ix = 0; ix < 10; ix++)
                              	{
                              		numbers.push_back(ix);
                              	}
                              	
                              	// Iterate through the vector and print out its values
                              	for(std::vector<int>::iterator iter = numbers.begin(); iter != numbers.end(); iter++)
                              	{
                              		std::cout << "Forward Iterate: " << *iter << std:: endl;
                              	}
                              
                              	// Iterate in reverse order through the vector and print out its values
                              	for(std::vector<int>::reverse_iterator riter = numbers.rbegin(); riter != numbers.rend(); riter++)
                              	{
                              		std::cout << "Reverse Iterate: " << *riter << std:: endl;
                              	}
                              	
                              	// Check size
                              	std::cout << "Count: " << numbers.size() << " = " << std::distance(numbers.begin(), numbers.end()) << std::endl;
                              	
                              	return 0;
                              }

                              Comment

                              Working...