(prompt "\nSquaring the circle by AutoCAD - SQR_CIRCLE.LSP") (prompt "\nCommands: SQR_CIRCLE (SPIRAL_A)") (prompt "\nFunctions: (LOCATE) (LINE_DISPLAY) (SETUP_ARCH) (SHOW_STATUS)") (prompt "\nFunctions: (CONVERGE) (ZOOM_REGEN) (RESULT_DISPLAY) (INIT_DISPLAY) ") (prompt "\n ") ;---------------------------------------------------------- ;Squaring the circle ;;;By Takaya Iwamoto Aug 9, 2000 ;;;SQR_CIRCLE.LSP ;;;Tangent line approach by Archimedes ;;;Interactive search trial ;; ;; ;Test --- one at a time (defun c:test () (setup_Arch) ;;setup (init_display) ;set initial position (while (setq pnt_temp (getpoint "\nPick a point on the positive X-axis. Hit return key to quit")) (locate pnt_temp) (line_display) (show_status) ) (result_display) (command "_.zoom" "_E") (command "_.regen") (reset_sysvar) );TEST ;; ;; ;Test_2-- Continuous pick (defun c:test_2 () (setup_Arch) ;;setup (init_display) ;set initial position (setq answer (getpoint "Place the cursor near the positive X-axis, then press left mouse to start.\n Press left mouse or return key to terminate." ) ) ;;; while loop to pick a point continuously (while (and (setq key (grread T)) (= (car key) 5)) (setq pnt_temp (cadr key) ) (locate pnt_temp) (line_display) (show_status) ) ;while (result_display) (command "_.zoom" "_E") (command "_.regen") (reset_sysvar) );TEST_2 ;;; ;; ;;; ;;;CONVERGE ;;; ;;;check the convergence comparing the radius and line segment OL (defun CONVERGE( / result) (setq diff_length (distance point_a point_b)) (if (< diff_length criteria) (setq result T) (setq result nil)) );CONVERGE ;;; ;;; ;;;SQR_CIRCLE ;;;main routine (defun c:SQR_CIRCLE() (setup_arch) ;setup parameters (init_display) ;set initial position (alert "Place the cursor in the 1-st Quadrant, then click left mouse to start.") (setq answer (getpoint "\nMove the mouse toward X-axis and Press left mouse for automatic zooming and regen.")) (terpri) (setq flag "GO" split_win "NO") (while (= flag "GO") (while (and (setq key (grread T)) (= (car key) 5) ) (setq pnt_tmp (cadr key)) (locate pnt_tmp) (line_display) (show_status) );inner while loop ;;;after the 2-nd round,work with 2 viewports. ;(if (= split_win "NO") (command "_.vports" "2" "v")) (if (= split_win "NO") (progn (command "_.vports" "3" "_L") (setvar "CVPORT" 2) (command "_.zoom" "_E") (setvar "CVPORT" 3) (command "_.zoom" "_E") (setvar "CVPORT" 4) (command "_.zoom" "_E") ) ) ;;;;convergence criteria check (if (= (converge) T) (setq flag "STOP")) ;;;;zoom and regen automatically for 2 viewports (zoom_regen pnt_pi pnt_x ) (setq split_win "YES") );outer while loop (make_line_1 "0" 2 point_a point_p) (entdel last_pline) (command "_.vports" "_SI" ) ;;;back to single view port (command "_.zoom" "_EXTENT") (regapp "my_point") (make_point "0" 0 pnt_pi "P" 3 0.20) (make_point "0" 0 pnt_org "O" 3 0.20) (make_point "0" 0 pnt_x "Q" 1 0.20) (result_display) (reset_sysvar) );SQR_CIRCLE ;;; ;;; ;;;ZOOM_REGEN ;;; (defun ZOOM_REGEN(pnt_target pnt_move) ;;;Target point (setq vscale (/ vscale 20.)) ;;;focus on the upper window (setvar "CVPORT" 3) (command "_.zoom" "ce" pnt_target vscale) (command "_.regen") ;;;Mouse control point (setq vscale (/ vscale 20.)) ;;;focus on the lower window (setvar "CVPORT" 4) (command "_.zoom" "ce" pnt_move vscale) (command "_.regen") );ZOOM_REGEN ;;; ;;;LOCATE ;;; (defun locate(pnt_temp / del_theta del_radius rad_oa rad_ob const_k) (setq del_theta (angle pnt_org pnt_temp) const_k (/ 0.5 pi) del_radius (* const_k del_theta) rad_oa (+ 1.0 del_radius) rad_ob (- 1.0 del_radius) point_a (polar pnt_org del_theta rad_oa) point_b (polar pnt_org (- del_theta) rad_ob) point_p (inters point_a point_b pnt_org '(0 -5) nil) ) );LOCATE ;;; ;;; ;;;LINE_DISPLAY ;;; (defun line_display() (if (/= last_pline nil) (entdel last_pline)) (command "_.pline" point_b pnt_org point_a point_p "") (setq last_pline (entlast)) );LINE_DISPLAY ;;; ;;;INIT_DISPLAY (defun init_display() (setq init_pos '(1.3 0.63)) (locate init_pos) (line_display) (setq last_pline (entlast)) (command "_.zoom" "e") );INIT_DISPLAY ;;; ;;;SETUP_arch ;;; setup_arch (defun SETUP_ARCH() (setup_sysvar) ;(set_layer) ;define layers (set_txstyle "arial") (setvar "PDMODE" 34) (setvar "PDSIZE" -3) (setq two_pi (* 2 pi) neg_2pi (- two_pi) ref_left (list -0.1 neg_2pi) ref_right (list 0.1 neg_2pi) ref_up '(1 0.001) ref_dn '(1 -0.001) ) (make_line_1 "0" 8 ref_left ref_right) (make_line_1 "0" 8 ref_dn ref_up) (make_line_1 "0" 8 '(-2 0) '(2 0)) (make_line_1 "0" 8 '(0 1.5) '(0 -7)) (spiral_a) (setq criteria 1.e-8 pnt_pi (list 0 (- (* 2 pi))) pnt_org '(0 0) pnt_x '(1 0) ) (textdisplay "Archimedes Spiral" '(-4.9050 1.1084) 0.3075 0.) (my_block_insert "arch_spiral" '(-4.5 0.7) 1.0) ;;;insert precision_meter ;block insert prec_meter.dwg (setq ins_pnt '(-4.0 -1.2) meter_scale 1.0 m_x (car ins_pnt) m_y (cadr ins_pnt) pm_factor (* 0.25 (/ 1. (log 10.))) ) (my_block_insert "prec_meter" ins_pnt meter_scale) ;display initial bar (my_block_insert "prec_bar" ins_pnt meter_scale) (setq old_bar (entlast)) ;;;prec_bar just created (command "_.zoom" "_EXTENT") (command "_.regen") (setq vscale (getvar "viewsize"));;;GET CURRENT VIEW SIZE );SETUP_ARCH ;;; ;;;SHOW_STATUS ;;; show the deviation from the exact solution in status line (defun SHOW_STATUS( / test_dist dif_dist result) (setq dist_ab (distance point_a point_b) pi_value (- (/ (cadr point_p) 2)) dev (abs (- pi pi_value)) result (strcat "dist= " (rtos dist_ab 2 10) " pi_dev= " (rtos dev 2 10)) ) (move_bar dev meter_scale) ;(grtext -1 result) );SHOW_STATUS ;;; ;;; ;;;CONVERGE ;;; ;;;check the convergence comparing the ref_rad and line length DE. (defun CONVERGE( / result) (setq test_length (distance point_p pnt_pi)) (if (< test_length criteria) (setq result T) (setq result nil)) );CONVERGE ;;; ;;; ;;;RESULT_DISPLAY ;;;display the results on the drawing window (defun RESULT_DISPLAY( / aob dob ratio angle_aob angle_dob angle_ratio deg_aob deg_dob result_title) (setq dist_ab (distance point_a point_b) pi_value (- (/ (cadr point_p) 2)) dev (abs (- pi pi_value)) result (strcat "dist= " (rtos dist_ab 2 10) " pi_dev= " (rtos dev 2 10)) ) (grtext -1 result) (setq line_1 "Result" line_2 (strcat "OP = " (rtos (distance pnt_org point_p) 2 10)) line_3 "Exact Value = 2 * pi" line_4 " 6.2831853072" line_5 (strcat "Area of Triangle OPQ = " (rtos pi_value 2 10)) line_7 "PQ is tangent to the curve at Q." line_6 "Exact value = 3.1415926536" ) (textdisplay line_1 '(-2.5 -1.5) 0.25 0) (textdisplay line_2 '(-3.5 -2.0) 0.20 0) (textdisplay line_3 '(-3.5 -2.5) 0.20 0) (textdisplay line_4 '(-3.5 -3.0) 0.20 0) (textdisplay line_5 '(-4.8 -4.0) 0.20 0) (textdisplay line_6 '(-4.0 -4.5) 0.20 0) (textdisplay line_7 '(-4.0 -5.5) 0.20 0) (textdisplay "Tangent to the Spiral" '(0.5 -5) 0.2 81.) );RESULT_DISPLAY ;;;SPIRAL_A ;Spiral_4-- Archimedian (defun SPIRAL_A () (setvar "PDMODE" 32) (setvar "PDSIZE" -1) (setup_sysvar) (setq const_a (* 0.5 (/ 1.0 pi)) pnt_origin '(0 0) theta_end (* 3.0 pi) del_theta 0.005 theta_old 0.0 pnt_old pnt_origin lower_left '(-2 -1.5) upper_right '(2 1.5) ) (command "_.zoom" "_W" lower_left upper_right) (while (<= (setq theta_new (+ theta_old del_theta)) theta_end) (setq radius (* const_a theta_new) pnt_new (polar pnt_origin theta_new radius) ) (make_line_1 "0" 4 pnt_old pnt_new) ;(command "_.delay" 1) (setq pnt_old pnt_new theta_old theta_new ) ) (setq last_line (entlast)) (command "_pedit" last_line "Yes" "Join" "All" "" "") ;(draw_x_axis y_ref x_from x_to step_x y_offset hx chr_height) (draw_x_axis 0.0 -2.0 2.0 0.5 -0.125 0.025 0.10) (draw_y_axis 0.0 -1.5 1.5 0.5 0.055 0.025 0.10) (command "_.zoom" "_EXTENT") (command "_.regen") ;;;; (reset_sysvar) (command "_ucsicon" "_off") ) ;;;SPIRAL_A ;Spiral_4-- Archimedian (defun C:SPIRAL_A () (setvar "PDMODE" 32) (setvar "PDSIZE" -1) (setup_sysvar) (setq const_a (* 0.5 (/ 1.0 pi)) pnt_origin '(0 0) theta_end (* 1.0 pi) del_theta 0.001 theta_old 0.0 pnt_old pnt_origin lower_left '(-2 -1.5) upper_right '(2 1.5) ) (command "_.zoom" "_W" lower_left upper_right) (while (<= (setq theta_new (+ theta_old del_theta)) theta_end) (setq radius (* const_a theta_new) pnt_new (polar pnt_origin theta_new radius) ) (make_line_1 "0" 4 pnt_old pnt_new) (command "_.delay" 1) (setq pnt_old pnt_new theta_old theta_new ) ) (setq last_line (entlast)) (command "_pedit" last_line "Yes" "Join" "All" "" "") ;(draw_x_axis y_ref x_from x_to step_x y_offset hx chr_height) (draw_x_axis 0.0 -2.0 2.0 0.5 -0.125 0.025 0.10) (draw_y_axis 0.0 -1.5 1.5 0.5 0.055 0.025 0.10) (command "_.zoom" "_EXTENT") (command "_.regen") ;;;; (reset_sysvar) (command "_ucsicon" "_off") ) ;;; ;;MAKE_anim_jpg ;;;make jpg files for animation (defun c:MAKE_anim_jpg() (setup_arch) (make_jpg) ;(setq nstep (getint "\nHow many steps:")) (setq nstep 10 step 0 pnt_start '(1.3 0.63) pnt_end '(0.999979 5.3417e-7) x_inc (/ 1. nstep) ) (init_display) (make_jpg) (repeat (1+ nstep) (setq pnt_temp (plt pnt_start pnt_end (* x_inc step)) ) (locate pnt_temp) (line_display) (command "_.delay" 500) (make_jpg) (setq step (1+ step)) ;(entdel line_ent) (show_status) (make_jpg) (command "_.delay" 1000) ); repeat loop (line_display) (make_jpg) (result_display) (make_jpg) (reset_sysvar) );MAKE_WMF ;;; ;;-------------------------------------------------------------------------- (princ)