- A+
想要通过点来判断路径的走向是顺时针还是逆时针,至少需要三个点来做判断,如果是两个点的话,是无法判断是顺时针还是逆时针。
下面直接粘贴lisp的判断3点的关系,是顺时还是逆时
通过三点判断是顺时针还是逆时针方向代码:
(defun C:hj1()
;|
设 pt1=(x1,y1), pt2=(x2,y2), pt3=(x3,y3)
求向量
pt1-2=(x2-x1,y2-y1)
pt2-3=(x3-x2,y3-y2)
则当 pt1-2 与 pt2-3 的叉乘(向量积)
pt1-2-3=pt1-2 x pt2-3 = (x2-x1)*(y3-y2)-(y2-y1)*(x3-x2)
为正时,pt1-2-3 走向为逆时针,
为负时,pt1-2-3 走向为顺时针,
为零时,pt1-2-3 所走的方向不变,亦即三点在一直线上
|;
(setq pt1 (getpoint "\n请指定点1:")
pt2 (getpoint "\n请指定点2:")
pt3 (getpoint "\n请指定点3:"))
(command "_pline" pt1 pt2 pt3 "");显示路径
(setq pt1-2(list(-(car pt2)(car pt1)) (-(cadr pt2)(cadr pt1))))
(setq pt2-3(list(-(car pt3)(car pt2)) (-(cadr pt3)(cadr pt2))))
(setq pt1-2-3(-(*(car pt1-2)(cadr pt2-3)) (*(cadr pt1-2)(car pt2-3))))
;(setq pt1-2-3(-( *(-(car pt2)(car pt1))(-(cadr pt3)(cadr pt2)))( *(-(cadr pt2)(cadr pt1))(-(car pt3)(car pt2)))))
(cond
((> pt1-2-3 0)(alert "逆时针"))
((< pt1-2-3 0)(alert "顺时针"))
((= pt1-2-3 0)(alert "3点在一条直线上"))
)
(princ)
)
复制
上面例子里面已注释是如何计算向量