Jump to content

Creating a new point from an offset of a point


MetalSutton

Recommended Posts

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. 

Link to comment
Share on other sites

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)

 

  • Like 1
Link to comment
Share on other sites

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)
) 

 

  • Like 2
Link to comment
Share on other sites

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.....

Link to comment
Share on other sites

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?

  • Like 1
Link to comment
Share on other sites

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:

 

image.png.af4310c20e050ded4cd43930bf8d37e4.png

 

 

 

Link to comment
Share on other sites

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:

 

image.png.af4310c20e050ded4cd43930bf8d37e4.png

 

 

 

image.png.523084c14801f511fae56cb23a4a81f7.png

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 by ronjonp
  • Like 2
Link to comment
Share on other sites

9 minutes ago, ronjonp said:

image.png.523084c14801f511fae56cb23a4a81f7.png

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. 

Link to comment
Share on other sites

Just now, MetalSutton said:

Awesome thanks. I agree mapcar and car etc, does look like the cleanest solution.

 

Thanks. 

🍻

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...