aloy 发表于 2022-7-5 16:01:36

点相对位置

大家好,
 
我有三个点A(x1,y1),B(x2,y2)和C(x3,y3)。当从A到B看时,我如何找到C的哪一边。也就是说,通过计算三个点的坐标,点C是在AB线的左侧还是右侧。
 
提前谢谢。
 
芦荟

Tharwat 发表于 2022-7-5 16:06:32

你好,阿洛伊,
 
请试试这个。

(and (setq a (getpoint "\nSpecify point A :"))
    (setq b (getpoint "\nSpecify point B :" a))
    (setq c (getpoint "\nSpecify point C :"))
    (alert (strcat "Point resides on the " (if (minusp (sin (- (angle a b) (angle a c)))) "Left" "Right") " Side."))
    )

aloy 发表于 2022-7-5 16:09:56

谢谢塔瓦。实际上,我希望通过一个程序分配三个点,以自动处理土方工程中挖方和填方的面积计算。我想我可以利用最后一行。
再次感谢。
 
芦荟

BIGAL 发表于 2022-7-5 16:13:03

还有一个向量答案类似于Tharwat解,我知道我用过它,但很难找到它,使用x&y
 
找到一个返回+或-答案的示例。现在找到我的lisp版本。
 

(p2.X-p1.X)*(p3.Y-p1.Y)-(p2.Y-p1.Y)*(p3.X-p1.X)

Tharwat 发表于 2022-7-5 16:15:33

 
欢迎你,祝你好运。

aloy 发表于 2022-7-5 16:20:36

嗨,比格尔,
我搜索时发现了类似的向量叉积。但我觉得它太长了。此外,它涉及行列式和转置等。感谢您指出。
 
芦荟

aloy 发表于 2022-7-5 16:23:03

嗨,塔瓦,
它工作得很好。我将发布代码和生成的图纸,用于自动计算切割和填充区域。
 
这里x2是挖方或填方末端的交点,q3是A,q4是B,pz是C。它给出了两个挖方和一个填方的总和。你为我节省了很多时间。
 
芦荟
(if x2 (progn (setq i2 (angle q3 q4)) (setq i3(angle q3 pz))))   
   (if (and x2 (and (> (sin (- i2 i3)) 0.0))) (setq fill (+ fill a)))
   (if (and x2 (and (< (sin (- i2 i3)) 0.0))) (setq cut (+ cut a)))

Lee Mac 发表于 2022-7-5 16:24:21

另一种方法是使用向量叉积:
(defun c:test ( / a b c )
   (if (and (setq a (getpoint "\n1st point of line: "))
            (setq b (getpoint "\n2nd point of line: " a))
            (setq c (getpoint "\nPoint to test: "))
       )
       (LM:clockwise-p a c b)
   )
)

;; Clockwise-p - Lee Mac
;; Returns T if p1,p2,p3 are clockwise oriented

(defun LM:clockwise-p ( a b c )
   (apply '> (mapcar '* (mapcar '- c a) (reverse (mapcar '- b a '(0 0)))))
)

aloy 发表于 2022-7-5 16:27:57

谢谢LM。我也会试试这个。我相信这会奏效的。但最后一行很难理解。

poulhein 发表于 2022-7-5 16:30:27

试试这个

;determines the orientation on the basis of pointaxis P1 -> P2
;Points in current UCS
;example function:

;(setq
;pt1 (getpoint "\nfirst point: ")
;pt2 (getpoint "\nsecond point: " pt1)

;(while
;(/= (car (setq pt3 (grread 'T 1 0))) 3)
;(if (leftright-p pt1 pt2 (cadr pt3))
;    (princ "\nyou are left: ")
;    (princ "\nyou are right: ")
;)

;returns T=left nil=right

(defun leftright-p
( pt1 pt2 pt3 /
   area
)
(setq area
   (+
   (- (* (car pt1) (cadr pt2)) (* (cadr pt1) (car pt2)))
   (- (* (cadr pt1) (car pt3)) (* (car pt1) (cadr pt3)))
   (- (* (car pt2) (cadr pt3)) (* (car pt3) (cadr pt2)))
   )
)
(and
   (> area 0)
   (not (equal area 0 0.0000001))
)
)
页: [1] 2
查看完整版本: 点相对位置