明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 1260|回复: 4

[已解答] 知道三角形的三边边长,然后画这个三角形,我写了代码,画出来不对,求大神解答!

[复制链接]
发表于 2013-12-1 16:17 | 显示全部楼层 |阅读模式
如题,知道三角形的三边边长,然后自动画出三角形!下面是我写的代码,求大神帮着看看,哪里不对!
  1. (defun c:tt()
  2.   (setq pt1 (getpoint "\n请指定三角形在屏幕中的位置:"))
  3.   (setq a (getreal "\n请输入三角形第一条边长:"))
  4.   (setq b (getreal "\n请输入三角形第二条边长:"))
  5.   (setq c (getreal "\n请输入三角形第三条边长:"))
  6.   (setq qq (/ (+ a b c) 2))
  7.   (setq s (sqrt (* qq (- qq a) (- qq b) (- qq c))))
  8.   (setq h (/ (* s 2) c))
  9.   (setq d (sqrt (- (* b b) (* h h))))
  10.   (setq jj (atan (/ h d)))
  11.   (setq pt2 (polar pt1 0 c))
  12.   (setq pt3 (polar pt1 jj b))
  13.   (command "pline" pt1 pt2 pt3 "")
  14. )


发表于 2013-12-1 18:37 | 显示全部楼层
发表于 2013-12-1 21:00 | 显示全部楼层
本帖最后由 edata 于 2013-12-1 21:52 编辑

  1. (defun c:tt(/ pt1 a b c qq s h d jj pt2 pt3);局部变量要设置
  2.   (setq pt1 (getpoint "\n请指定三角形在屏幕中的位置:"))
  3.   (setq a (getreal "\n请输入三角形第一条边长:"))
  4.   (setq b (getreal "\n请输入三角形第二条边长:"))
  5.   (setq c (getreal "\n请输入三角形第三条边长:"))
  6.   (if (and pt1 a b c (> (+ a b ) c)(< (- a b) c))(princ "\n是三角形")(princ "\n不是三角形"));判断方式一
  7.   (if (and pt1 a b c (> (+ b c) a)(> (+ a c) b)(> (+ a b) c) );判断方式二
  8.     (progn
  9.   (setq qq (/ (+ a b c) 2.0))
  10.   (setq s (sqrt (* qq (- qq a) (- qq b) (- qq c))))
  11.   (setq h (/ (* s 2.0) c))
  12.   (setq d (sqrt (- (* b b) (* h h))))
  13.   (setq jj (atan (/ h d)))
  14.   (setq pt2 (polar pt1 0.0 c))
  15.   (setq pt3 (polar pt1 jj b))
  16.   (command "pline" "non" pt1 "non" pt2 "non" pt3 "c" );指定点的时候加"non"是临时取消捕捉,避免因为捕捉造成绘图失败
  17.   )
  18.     (cond ;判断那一个输入值出错的一种方式
  19.       ((null pt1)(princ "\n未指定点"))
  20.       ((or (null a)(null b)(null c ))(princ "\n未输入边长"))
  21.     (t (princ "\n不是三角形"))
  22.     )
  23.     )
  24.   (princ)
  25. )
发表于 2013-12-1 21:41 | 显示全部楼层
本帖最后由 edata 于 2013-12-1 21:52 编辑

再加一种算法,还有一种可以用圆的交点来求三点,平时画图就是用圆交点来画的,有空再弄。
  1. (defun c:tt(/ pt1 a b c qq s h d jj pt2 pt3 xd xh ptx1 ptx2 ptxd);局部变量要设置
  2.   (setq pt1 (getpoint "\n请指定三角形在屏幕中的位置:"))
  3.   (setq a (getreal "\n请输入三角形第一条边长:"))
  4.   (setq b (getreal "\n请输入三角形第二条边长:"))
  5.   (setq c (getreal "\n请输入三角形第三条边长:"))
  6.   (if (and pt1 a b c (> (+ a b ) c)(< (- a b) c))(princ "\n是三角形")(princ "\n不是三角形"));判断方式一
  7.   (if (and pt1 a b c (> (+ b c) a)(> (+ a c) b)(> (+ a b) c) );判断方式二
  8.     (progn
  9.       (setq xd(/ (- (+ (* a a) (* c c))  (* b b) ) (* a 2.0)));三角形算法二开始
  10.       (setq xh(sqrt (- (* c c) (* xd xd))))
  11.       (princ xd )(princ "\n")(princ xh)
  12.       (setq ptx1(polar pt1 0 a)
  13.             ptxd(polar pt1 0 xd)
  14.             ptx2 (polar ptxd (* pi 0.5) xh));三角形算法二结束
  15.       (command "line" "non" pt1 "non" ptx1 "non" ptx2 "c")
  16.   (setq qq (/ (+ a b c) 2.0))
  17.   (setq s (sqrt (* qq (- qq a) (- qq b) (- qq c))))
  18.   (setq h (/ (* s 2.0) c))
  19.   (setq d (sqrt (- (* b b) (* h h))))
  20.   (setq jj (atan (/ h d)))
  21.   (setq pt2 (polar pt1 0.0 c))
  22.   (setq pt3 (polar pt1 jj b))
  23.   (command "pline" "non" pt1 "non" pt2 "non" pt3 "c" );指定点的时候加"non"是临时取消捕捉,避免因为捕捉造成绘图失败
  24.   )
  25.     (cond ;判断那一个输入值出错的一种方式
  26.       ((null pt1)(princ "\n未指定点"))
  27.       ((or (null a)(null b)(null c ))(princ "\n未输入边长"))
  28.     (t (princ "\n不是三角形"))
  29.     )
  30.     )
  31.   (princ)
  32. )

评分

参与人数 1明经币 +1 收起 理由
312735894 + 1 感谢大大帮助

查看全部评分

 楼主| 发表于 2013-12-1 21:59 | 显示全部楼层
呵呵 谢谢大大帮助!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-5-19 22:17 , Processed in 0.302925 second(s), 34 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表