Author Topic: Geometric/mathematical question  (Read 4021 times)

0 Members and 1 Guest are viewing this topic.

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1451
  • Marco
Geometric/mathematical question
« on: September 01, 2018, 10:14:52 AM »
All 2D points
Code: [Select]
(defun DTR (ang)(* pi (/ ang 180.0)))

Sample A:
(setq P1 '(200.0 300.0) P2 '(400.0 400.0))

(setq P3 (list (+ (car P1) 30) (+ (cadr P1) 20))) => (230.0 320.0)
(setq P4 (list (+ (car P1) 90) (- (cadr P2) 53))) => (290.0 347.0)
(setq P5 (list (- (car P2) 40) (- (cadr P2) 35))) => (360.0 365.0)

Sample B:
(setq A1 310);angle
(setq P1 '(200.0 300.0)  P2 '(111.07,94.84)) ; (rounded)

(setq P3 (polar (polar P1 (DTR (- 310 90)) 30) (DTR 310) 20)) => (189.874 265.395)
...

Maybe it's a stupid question: is there a simpler way to calculate points in sample B?

ribarm

  • Gator
  • Posts: 3256
  • Marko Ribar, architect
Re: Geometric/mathematical question
« Reply #1 on: September 01, 2018, 11:04:46 AM »
Maybe it's not simpler, but sure is more elegant...
You could use matrix with rotated coordinate system and then multiply each vector of p3, p4, p5 with it to get new coordinates... You can also acquire this result by setting UCS for Sample A rectangle (translated WCS to UCS at p1) and use (trans) function to translate point coordinates from WCS to UCS at p1 - you use (trans p 0 1) and finally from those resulting points in UCS (translated) to WCS, but previously set new Sample B UCS (rotated) - you then use (trans p-"sampleA-previousUCS(translated)" 1-"sampleB-UCS(rotated)" 0)...
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1451
  • Marco
Re: Geometric/mathematical question
« Reply #2 on: September 01, 2018, 11:22:10 AM »
Maybe it's not simpler, but sure is more elegant...
You could use matrix with rotated coordinate system and then multiply each vector of p3, p4, p5 with it to get new coordinates... You can also acquire this result by setting UCS for Sample A rectangle (translated WCS to UCS at p1) and use (trans) function to translate point coordinates from WCS to UCS at p1 - you use (trans p 0 1) and finally from those resulting points in UCS (translated) to WCS, but previously set new Sample B UCS (rotated) - you then use (trans p-"sampleA-previousUCS(translated)" 1-"sampleB-UCS(rotated)" 0)...
Thanks for answer, I tried with the change of UCS but it is a too slow operation if repeated many many times. Have you an example with matrix?

ribarm

  • Gator
  • Posts: 3256
  • Marko Ribar, architect
Re: Geometric/mathematical question
« Reply #3 on: September 01, 2018, 11:57:41 AM »
Code - Auto/Visual Lisp: [Select]
  1. (defun c:sample_A-B ( / mxv p1 p2u p3u p4u p5u plst plstA mat plstB )
  2.  
  3.   ;; Matrix x Vector - Vladimir Nesterovsky
  4.   ;; Args: m - nxn matrix, v - vector in R^n
  5.  
  6.   (defun mxv ( m v )
  7.     (mapcar '(lambda ( r ) (apply '+ (mapcar '* r v))) m)
  8.   )
  9.  
  10.   (setq p1 '(200.0 300.0 0.0))
  11.   (setq p2u '(200.0 100.0 0.0))
  12.   (setq p3u '(30.0 20.0 0.0))
  13.   (setq p4u '(90.0 47.0 0.0))
  14.   (setq p5u '(160.0 65.0 0.0))
  15.   (setq plst (list '(0.0 0.0 0.0) p2u p3u p4u p5u))
  16.   (setq plstA (mapcar '(lambda ( x ) (mapcar '+ p1 x)) plst))
  17.   (setq mat (list (list (cos (cvunit 220 "degree" "radian")) (- (sin (cvunit 220 "degree" "radian"))) 0.0) (list (sin (cvunit 220 "degree" "radian")) (cos (cvunit 220 "degree" "radian")) 0.0) (list 0.0 0.0 1.0)))
  18.   (setq plstB (mapcar '(lambda ( x ) (mapcar '+ p1 (mxv mat x))) plst))
  19.   (prompt "\nSample A : ") (princ plstA)
  20.   (prompt "\nSample B : ") (princ plstB)
  21.   (princ)
  22. )
  23.  

HTH., M.R.
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

Stefan

  • Bull Frog
  • Posts: 319
  • The most I miss IRL is the Undo button
Re: Geometric/mathematical question
« Reply #4 on: September 01, 2018, 03:37:07 PM »
Maybe this

Code - Auto/Visual Lisp: [Select]
  1. ;rotate a point around 0,0, a radians ccw
  2. (defun rot (p a)
  3.   (list
  4.     (- (* (car p) (cos a)) (* (cadr p) (sin a)))
  5.     (+ (* (car p) (sin a)) (* (cadr p) (cos a)))
  6.   )
  7. )
  8.  
  9. ;rotating a point pct around origin point, ang radians ccw
  10. (defun _trans (pct orig ang)
  11.   (mapcar '+ orig
  12.     (rot (mapcar '- pct orig) ang)
  13.   )
  14. )
And the results on your sample
Code - Auto/Visual Lisp: [Select]
  1. (setq orig '(200 300) ; p1
  2.       ang  (/ (* 11.0 pi) 9.0) ; 220 deg, ccw (trigonometric)
  3. )
  4.  
  5. P3
  6. (_trans '(230 320) orig ang) -> (189.874 265.395)
  7.  
  8.  
  9. P4
  10. (_trans '(290 347) orig ang) -> (161.267 206.145)
  11.  
  12.  
  13. P5
  14. (_trans '(360 365) orig ang) -> (119.214 147.361)

BIGAL

  • Swamp Rat
  • Posts: 1409
  • 40 + years of using Autocad
Re: Geometric/mathematical question
« Reply #5 on: September 01, 2018, 07:00:16 PM »
My interpretation, pick control pt, pick 2nd point, enter x & Y done. using the 2nd point for the known angle its simple to add 1/2 pi to the angle when doing the polar calcs.

Code: [Select]
(defun test ( / oldsnap x y pt1 pt2 ang)
(setq oldsnap (getvar 'osmode))
(setq pt1 (getpoint "pick pt1"))
(setq pt2 (getpoint "Pick pt2"))
(setq ang (angle pt1 pt2))
(setq x (getreal "dist X"))
(setq y (getreal "dist Y"))
(setq pt2 (polar pt1 ang x))
(setq pt2 (polar pt2 (+ ang (/ pi 2)) y))
(setvar 'osmode 0)
(command "point" pt2)
(setvar 'osmode oldsnap)
)
(test)
A man who never made a mistake never made anything

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1451
  • Marco
Re: Geometric/mathematical question
« Reply #6 on: September 02, 2018, 01:10:24 AM »
@Stefan: This is exactly what I needed to calc rot only one time.
@Marko: the example of is also very useful.

Based on BIGAL sample:
Code: [Select]
(setq
  orig '(200.0 300.0 0.0)  ; p1
  ang  (/ (* 11.0 pi) 9.0) ; 220 deg, ccw (trigonometric)
)
(defun TransByAngOff (OrgPnt RotAng OffStX OffStY)
  (polar (polar OrgPnt RotAng OffStX) (+ RotAng (/ pi 2)) OffStY)
)
(TransByAngOff orig ang 30 20) => (189.874 265.395 0.0)


New question: is the use of polar more efficient? or is better Stefan/Marko approach?

Stefan

  • Bull Frog
  • Posts: 319
  • The most I miss IRL is the Undo button
Re: Geometric/mathematical question
« Reply #7 on: September 02, 2018, 03:59:31 AM »
You know what? The polar approach is the most simple and direct solution.
What I'd change, is the input convention. OffStX and OffStY are just the coordinates of the current point relative to P1.
IMO, using points in WCS is better and if you really need the relative coordinates, you can always use (mapcar '- p o).
In this case, your function could be:
o - origin (P1 in your case)
p - point, in WCS
a - rotation angle
Code - Auto/Visual Lisp: [Select]
  1. (polar o (+ (angle o p) a) (distance o p))

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1451
  • Marco
Re: Geometric/mathematical question
« Reply #8 on: September 02, 2018, 04:27:22 AM »
You know what? The polar approach is the most simple and direct solution.
What I'd change, is the input convention. OffStX and OffStY are just the coordinates of the current point relative to P1.
IMO, using points in WCS is better and if you really need the relative coordinates, you can always use (mapcar '- p o).
In this case, your function could be:
o - origin (P1 in your case)
p - point, in WCS
a - rotation angle
Code - Auto/Visual Lisp: [Select]
  1. (polar o (+ (angle o p) a) (distance o p))
Thanks for your time, If I know offset as in my drawing image for P3, are you saying that is better this approach?
Code: [Select]
(setq P1 '(200.0 300.0))
(setq ang  (/ (* 11.0 pi) 9.0))
(defun TransByPntAng (o p a)
  (polar o (+ (angle o p) a) (distance o p))
)
(TransByPntAng P1 (mapcar '+ '(30 20 0) P1) ang) => (189.874 265.395)

GP

  • Newt
  • Posts: 83
  • Vercelli, Italy
Re: Geometric/mathematical question
« Reply #9 on: September 02, 2018, 12:20:41 PM »
Also…

with:
(setq p1 '(200.0 300.0))
(setq p5 '(360.0 365.0))
(setq a 310.0)

(setq p5r (cal "rot (p5,p1,a-90)"))

(119.214 147.361 0.0)
_$

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1451
  • Marco
Re: Geometric/mathematical question
« Reply #10 on: September 03, 2018, 08:41:43 AM »
Also…

with:
(setq p1 '(200.0 300.0))
(setq p5 '(360.0 365.0))
(setq a 310.0)

(setq p5r (cal "rot (p5,p1,a-90)"))

(119.214 147.361 0.0)
_$
Thanks, CAL is not an option for BricsCAD  :(

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Geometric/mathematical question
« Reply #11 on: September 08, 2018, 03:18:45 PM »
Oldie:
Code: [Select]
;|TEXTBOX returns the points that enclose the text relative to 0,0.  So after
*translating* the points returned to be relative to the insertion point of
the text, I have done this for 2d purposes as far as rotation is concerned.
--
-Jason
|;


Jason Piercey   Apr 9 2002, 7:40 pm
;Rotate a point or list of points about a base point
;ArgLst '([Base point] [List of points] [Radian angle])
(defun PtRotate (ArgLst / BasePt PtLst Ang d a)
  (mapcar 'set '(BasePt PtLst Ang) ArgLst)
  (mapcar
    '(lambda (x)
       (setq d (distance BasePt x)
             a (+ (angle BasePt x) Ang)
             )
       (polar BasePt a d)
       )
    PtLst
    )
  )

I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1451
  • Marco
Re: Geometric/mathematical question
« Reply #12 on: September 10, 2018, 11:54:04 AM »
Oldie:
Code: [Select]
;|TEXTBOX returns the points that enclose the text relative to 0,0.  So after
*translating* the points returned to be relative to the insertion point of
the text, I have done this for 2d purposes as far as rotation is concerned.
--
-Jason
|;


Jason Piercey   Apr 9 2002, 7:40 pm
;Rotate a point or list of points about a base point
;ArgLst '([Base point] [List of points] [Radian angle])
(defun PtRotate (ArgLst / BasePt PtLst Ang d a)
  (mapcar 'set '(BasePt PtLst Ang) ArgLst)
  (mapcar
    '(lambda (x)
       (setq d (distance BasePt x)
             a (+ (angle BasePt x) Ang)
             )
       (polar BasePt a d)
       )
    PtLst
    )
  )

It is also useful, I prefer this form:
Code: [Select]
(defun PtRotate2 (BasePt PtLst Ang / d a)
  (mapcar
   '(lambda (x)
       (setq d (distance BasePt x)
             a (+ (angle BasePt x) Ang)
       )
       (polar BasePt a d)
    )
    PtLst
  )
)

(setq  ArgLst  (list '(10. 10. 0.) '((20. 20. 0.) (30. 30. 0.) (30. 30. 0.)) (/ pi 2)))
(setq BasePt '(10. 10. 0.) PtLst '((20. 20. 0.) (30. 30. 0.) (30. 30. 0.)) Ang (/ pi 2))

Command: (PtRotate ArgLst)             => ((0.0 20.0 0.0) (-10.0 30.0 0.0) (-10.0 30.0 0.0))
Comamnd: (PtRotate2 BasePt PtLst Ang)  => ((0.0 20.0 0.0) (-10.0 30.0 0.0) (-10.0 30.0 0.0))