MetalSutton Posted July 27, 2022 Posted July 27, 2022 Hi all. I am on my phone right now so I cannot post code, however I am curious as to how I can access the x and y coordinates of a point to do math on them. For example, in order to get the 4 points of a rectangle, that's easy to do if you have user prompts to enter the width and height. But if I want the user to define the bot left and top right points with a click. I now have 2 points defined but now cannot use polar to find the other two points because I still don't know width and height. A point is a x and y coordinate, so what's the syntax for running math on the coordinates to be able to define new points. Thanks. Quote
mhupp Posted July 27, 2022 Posted July 27, 2022 This will give you the length and width from two points UR (4 6) LL (2 2) (setq L&W (mapcar '- UR LL)) L&W = (2 4) lenght = (car L&W) width = (cadr L&W) or pull x and y from defined points (setq UL (list (car LL) (cadr UR))) UL = (2 6) (setq LR (list (car UR) (cadr LL))) LR = (4 2) 1 Quote
Trudy Posted July 27, 2022 Posted July 27, 2022 Hello, something from me . (defun c:kk (/ PT1 PT2 lower-left upper-right upper-left lower-right) (setq PT1 (getpoint "Select first point: ")) (setq PT2 (getcorner PT1 "\nSelect second point: ")) (setq lower-left (list (apply 'min (list (car PT1) (car PT2))) (apply 'min (list (cadr PT1) (cadr PT2))))) (setq upper-right (list (apply 'max (list (car PT1) (car PT2))) (apply 'max (list (cadr PT1) (cadr PT2))))) (setq lower-right (list (nth 0 upper-right) (nth 1 lower-left))) (setq upper-left (list (nth 0 lower-left) (nth 1 upper-right))) (princ (strcat "\nLower-left--> "(rtos (car lower-left) 2 3) " "(rtos (cadr lower-left) 2 3))) (princ (strcat "\nUpper-left--> "(rtos (car upper-left) 2 3)" "(rtos (cadr upper-left) 2 3))) (princ (strcat "\nUpper-right--> "(rtos (car upper-right) 2 3)" "(rtos (cadr upper-right) 2 3))) (princ (strcat "\nLower-right--> "(rtos (car lower-right) 2 3)" "(rtos (cadr lower-right) 2 3))) (princ) ) 2 Quote
Steven P Posted July 27, 2022 Posted July 27, 2022 mapcar is far neater way to calculate the lengths. If you need to use the lengths then you might need an (abs..... ) ? For points this is not needed since it will work out if the user picks them in the wrong order, UR instead of LL for example. To throw a problem at this, what happens if the rectangle isn't lined up with the axis..... Quote
mhupp Posted July 27, 2022 Posted July 27, 2022 10 minutes ago, Steven P said: To throw a problem at this, what happens if the rectangle isn't lined up with the axis..... That's a good point, but I am assuming if your asking the user to select the points it has to be horizontal, because their isn't anyway to tell the angle of the rectangle by just two points. maybe that is what @MetalSutton is asking about? 1 Quote
MetalSutton Posted July 27, 2022 Author Posted July 27, 2022 What I mean is, can you do something like this: ;create new point from x and y offset of point. (setq off1 (pt1 + (12,12)) The rectangle example is: Quote
ronjonp Posted July 27, 2022 Posted July 27, 2022 (edited) 21 minutes ago, MetalSutton said: What I mean is, can you do something like this: ;create new point from x and y offset of point. (setq off1 (pt1 + (12,12)) The rectangle example is: IE: (setq pt1 '(0 0) pt2 '(150 250) pt3 (mapcar '+ (list 0 (cadr pt2))) pt4 (mapcar '+ (list (car pt2) 0)) ;; or less 'fancy' pt3 (list (car pt1) (+ (cadr pt1) (cadr pt2))) pt4 (list (+ (car pt1) (car pt2)) (cadr pt1)) ) Edited July 27, 2022 by ronjonp 2 Quote
MetalSutton Posted July 27, 2022 Author Posted July 27, 2022 9 minutes ago, ronjonp said: IE: (setq pt1 '(0 0) pt2 '(150 250) pt3 (mapcar '+ (list 0 (cadr pt2))) pt4 (mapcar '+ (list (car pt2) 0)) ;; or less 'fancy' pt3 (list (car pt1) (+ (cadr pt1) (cadr pt2))) pt4 (list (+ (car pt1) (car pt2)) (cadr pt1)) ) Awesome thanks. I agree mapcar and car etc, does look like the cleanest solution. Thanks. Quote
ronjonp Posted July 27, 2022 Posted July 27, 2022 Just now, MetalSutton said: Awesome thanks. I agree mapcar and car etc, does look like the cleanest solution. Thanks. Quote
mhupp Posted July 28, 2022 Posted July 28, 2022 15 hours ago, MetalSutton said: ;create new point from x and y offset of point. (setq off1 (pt1 + (12,12)) (setq off1 (mapcar '+ pt1 '(12 12))) (setq off2 (mapcar '- pt1 '(12 12))) pt1 = (10 10) off1 = (22 22) off2 = (-2 -2) Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.