Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Question about CPoint objects...

Question about CPoint objects...

Scheduled Pinned Locked Moved C / C++ / MFC
c++helpquestiondata-structurestutorial
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    CDuddley
    wrote on last edited by
    #1

    Hi I'm new to this forum, but I think it's pretty cool. I had a question about CPoint object. I need to be able to randomly generate an array of them. Which I got that far, but my problem was when the user entered a point, and I was supposed to tell the program to find the closest point in the array. Do any of you know how to do that? I tried using like the '<' compare operator but the CPoint class doesn't have that operator. Don't worry I'm not trying to get free homework (I'm using Introduction to MFC with Visual C++ to learn MFC) or anything I just don't understand and I would like to understand it. Thanks in advance for your help. -CDudd

    C P 2 Replies Last reply
    0
    • C CDuddley

      Hi I'm new to this forum, but I think it's pretty cool. I had a question about CPoint object. I need to be able to randomly generate an array of them. Which I got that far, but my problem was when the user entered a point, and I was supposed to tell the program to find the closest point in the array. Do any of you know how to do that? I tried using like the '<' compare operator but the CPoint class doesn't have that operator. Don't worry I'm not trying to get free homework (I'm using Introduction to MFC with Visual C++ to learn MFC) or anything I just don't understand and I would like to understand it. Thanks in advance for your help. -CDudd

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      You need to use < on the members of CPoint, which are x and y. And I'd answer this question if it *was* homework, because you're trying to do something, not just asking for a complete solution on a plate. Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little.

      C 1 Reply Last reply
      0
      • C Christian Graus

        You need to use < on the members of CPoint, which are x and y. And I'd answer this question if it *was* homework, because you're trying to do something, not just asking for a complete solution on a plate. Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little.

        C Offline
        C Offline
        CDuddley
        wrote on last edited by
        #3

        I have tried that. I'm not really sure where to go from there. Another hint please? I'm not trying to do 'anything' I'm just trying to learn. Thanks for the quick post. -CDudd

        1 Reply Last reply
        0
        • C CDuddley

          Hi I'm new to this forum, but I think it's pretty cool. I had a question about CPoint object. I need to be able to randomly generate an array of them. Which I got that far, but my problem was when the user entered a point, and I was supposed to tell the program to find the closest point in the array. Do any of you know how to do that? I tried using like the '<' compare operator but the CPoint class doesn't have that operator. Don't worry I'm not trying to get free homework (I'm using Introduction to MFC with Visual C++ to learn MFC) or anything I just don't understand and I would like to understand it. Thanks in advance for your help. -CDudd

          P Offline
          P Offline
          Peter Godec
          wrote on last edited by
          #4

          CDuddley wrote: to find the closest point If i understand correctly, you need to define a measure of distance between CPoints to do that. Most probably that would be euclidean distance , i.e. d = sqrt((x1 - x2)^2 + (y1 - y2)^2) So define a function that calculates the euclidean distance between two CPoints, maybe something like this:

          #include <math.h> // or <cmath> and std:: if your compiler/library isn't broken

          double distance(const CPoint& p1, const CPoint& p2)
          {
          return sqrt((p1.x - p2.x)*(p1.x - p2.x) + (p1.y - p2.y)*(p1.y - p2.y));
          }

          a nicer variant (performance?) of this would be

          double distance(const CPoint& p1, const CPoint& p2)
          {
          CSize diff = p1 - p2;
          return sqrt(diff.x * diff.x + diff.y * diff.y);
          }

          (of course, for your purpose, you don't need to calculate the sqrt, but now i'm blabbering) Now all you's gotta do is iterate over the array of CPoints, calculating the distance between your given test point and each point in the array and keeping track of the array index of the point that is the closest to the test point (now read this paragraph again). that-exercise-is-left-to-the-OP-ly y'rs --pg :)

          C 2 Replies Last reply
          0
          • P Peter Godec

            CDuddley wrote: to find the closest point If i understand correctly, you need to define a measure of distance between CPoints to do that. Most probably that would be euclidean distance , i.e. d = sqrt((x1 - x2)^2 + (y1 - y2)^2) So define a function that calculates the euclidean distance between two CPoints, maybe something like this:

            #include <math.h> // or <cmath> and std:: if your compiler/library isn't broken

            double distance(const CPoint& p1, const CPoint& p2)
            {
            return sqrt((p1.x - p2.x)*(p1.x - p2.x) + (p1.y - p2.y)*(p1.y - p2.y));
            }

            a nicer variant (performance?) of this would be

            double distance(const CPoint& p1, const CPoint& p2)
            {
            CSize diff = p1 - p2;
            return sqrt(diff.x * diff.x + diff.y * diff.y);
            }

            (of course, for your purpose, you don't need to calculate the sqrt, but now i'm blabbering) Now all you's gotta do is iterate over the array of CPoints, calculating the distance between your given test point and each point in the array and keeping track of the array index of the point that is the closest to the test point (now read this paragraph again). that-exercise-is-left-to-the-OP-ly y'rs --pg :)

            C Offline
            C Offline
            CDuddley
            wrote on last edited by
            #5

            Thanks a bunch for the post. I was having trouble with the fact that if a random point was greater than the given point the point would be negative when I took the difference. I forgot all about the euclidian distance formula thanks a ton! I'll try to get the test program working now! Thanks again, CDudd

            1 Reply Last reply
            0
            • P Peter Godec

              CDuddley wrote: to find the closest point If i understand correctly, you need to define a measure of distance between CPoints to do that. Most probably that would be euclidean distance , i.e. d = sqrt((x1 - x2)^2 + (y1 - y2)^2) So define a function that calculates the euclidean distance between two CPoints, maybe something like this:

              #include <math.h> // or <cmath> and std:: if your compiler/library isn't broken

              double distance(const CPoint& p1, const CPoint& p2)
              {
              return sqrt((p1.x - p2.x)*(p1.x - p2.x) + (p1.y - p2.y)*(p1.y - p2.y));
              }

              a nicer variant (performance?) of this would be

              double distance(const CPoint& p1, const CPoint& p2)
              {
              CSize diff = p1 - p2;
              return sqrt(diff.x * diff.x + diff.y * diff.y);
              }

              (of course, for your purpose, you don't need to calculate the sqrt, but now i'm blabbering) Now all you's gotta do is iterate over the array of CPoints, calculating the distance between your given test point and each point in the array and keeping track of the array index of the point that is the closest to the test point (now read this paragraph again). that-exercise-is-left-to-the-OP-ly y'rs --pg :)

              C Offline
              C Offline
              CDuddley
              wrote on last edited by
              #6

              You're awesome I got the exercise working without a hitch! Thanks a ton man. -CDudd

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups