;;;Summation of integers ;;;sumint_1 ;;; Sept 19,2003--Sept 22,2003 ;;; Copyright 2006 Takaya Iwamoto ;;; (defun c:sumint_1 () (setq nstep 1) (setup_sumint) (while (= (Yes_or_No "Go to next step ?") "_Y") (draw_ball nstep col_id) (draw_fence nstep) (draw_text nstep) (IF (= JPG_Yes 1) (make_jpg) ) (setq nstep (1+ nstep)) ) ;;;end of while loop (draw_final_text) (IF (= JPG_YES 1) (make_jpg) ) (alert "\n********End*******\nIt is clear that \nsum of integers from 1 to n \nis 1/2*N*(N+1)." ) (reset_sysvar) ) ;;; (defun setup_sumint ( / py pxy px lower_left upper_right y_text3) (setup_sysvar) (if (= ICAD 0) (command "shademode" "G")) ;;;draw the first dot and lines (setq py (list nstep nstep) pxy (list nstep 0) px (list 0 0) lower_left '(-2 -2.25) upper_right '(12 12) dot_rad 0.35 x_text1 -1.3 y_text2 -1.0 y_text3 -2.0 nstep (1+ nstep) ) ;;;draw a ball (my_block_insert "white_ball" '(0.5 0.5) dot_rad) (my_block_insert "red_ball" '(1.5 0.5) dot_rad) ;;;draw a fence (command "_.pline" px pxy py "") (textdisplay "N" (list x_text1 y_text2) 0.4 0.) (textdisplay "1" (list x_text1 0.25) 0.4 0.) (textdisplay "1" (list 1.3 y_text2) 0.4 0.) (command "_.zoom" "W" lower_left upper_right) (IF (= JPG_YES 1) (make_jpg) ) ) ;; ;;; ;;;DRAW_DOT draw dots (2n-1) = (n-1) + (n-1) + 1 ;;; (n-1) each horizontally & vertically at x & y = (n-0.5) ;;; remaining one at x = y = (n-0.5) ;;; (defun draw_dot (num col_id / ns coord x0 y0 delta step x_loc y_loc xy_loc) (setq ns (1- num) coord (- num 0.5) x0 0.5 y0 0.5 xy_loc (list coord coord) delta 1.0 step 0 ) (repeat ns (setq x_loc (list coord (+ y0 (* step delta))) y_loc (list (+ x0 (* step delta)) coord) ) (circle_dot "0" col_id x_loc dot_rad) (circle_dot "0" col_id y_loc dot_rad) (setq step (1+ step)) ) ;;;loop for repeat (circle_dot "0" col_id xy_loc dot_rad) ) ;;;DRAW_DOT ;;; ;;; ;;;DRAW_BALL draw balls (2n-1) = (n-1) + (n-1) + 1 ;;; (n-1) each horizontally & vertically at x & y = (n-0.5) ;;; remaining one at x = y = (n-0.5) ;;; (defun draw_ball (num col_id / ns step w_loc r_loc w_ys r_xs) (setq ns num step 0 w_ys (- num 0.5) r_xs (+ num 0.5) ) ;;;draw white & red balls num number of times (repeat ns (setq w_loc (list (+ step 0.5) w_ys)) (my_block_insert "white_ball" w_loc dot_rad) (setq step (1+ step)) ) ;;;loop for repeat (setq step 0) (repeat ns (setq r_loc (list r_xs (+ step 0.5))) (my_block_insert "red_ball" r_loc dot_rad) (setq step (1+ step)) ) ;;;loop for repeat ) ;;;DRAW_DOT ;;; ;;; ;;;DRAW_FENCE ;;; (defun draw_fence (num / px py pxy) ;;;draw the first dot and lines (setq px (list (1- num) (1- num)) pxy (list num (1- num)) py (list num num) ) ;;;draw a fence (command "_.pline" px pxy py "") ) ;;;DRAW_FENCE ;;; ;;; ;;;DRAW_TEXT ;;; (defun draw_text (num / loc1 loc2 loc2 w_ys r_xs) (setq w_ys (- num 0.5) r_xs (+ num 0.5) loc1 (list x_text1 w_ys) loc2 (list r_xs y_text2) ) (textdisplay (itoa num) loc1 0.4 0.) (textdisplay (itoa num) loc2 0.4 0.) ) ;;;draw_text ;;; ;;; CIRCLE_DOT colored circle dot display ;;; (defun CIRCLE_DOT (layername color_id center_pt dot_radius) (setq cur_col (getvar "cecolor")) (setvar "cecolor" (itoa color_id)) (make_circle_1 layername color_id center_pt dot_radius) (command "_.hatch" "_solid" (entlast) "") (setvar "cecolor" cur_col) ) ;;;CIRCLE_DOT ;;; ;;;DRAW_FINAL_TEXT ;;; (defun draw_final_text ( / final_text1 final_text2 final_text3 final_loc1 final_loc2 final_loc3 fin_x fin_y pfx pfy pfxr pfyr pfl pflu pfyu ) (setq final_text1 "Sum of integers" final_text2 "from 1 to N" final_text3 "is 1/2* N (N + 1)" final_loc1 '(-7 6) final_loc2 '(-7 5) final_loc3 '(-7 4) fin_y (- nstep 1) fin_x (+ 1 fin_y) pfx (list fin_x 0) pfy (list fin_x fin_y) pfxr (list (+ 2 fin_x) 0) pfyr (list (+ 2 fin_x) fin_y) pfl (list 0 fin_y) pflu (list 0 (+ 2 fin_y)) pfyu (list fin_x (+ 2 fin_y)) ) (textdisplay final_text1 final_loc1 0.45 0.) (textdisplay final_text2 final_loc2 0.45 0.) (textdisplay final_text3 final_loc3 0.45 0.) (make_line_1 "0" 8 pfy pfyr) (make_line_1 "0" 8 pfx pfxr) (make_line_1 "0" 8 pfxr pfyr) (make_line_1 "0" 8 pfl pflu) (make_line_1 "0" 8 pfy pfyu) (make_line_1 "0" 8 pflu pfyu) (textdisplay "N + 1" (list (- (* 0.5 fin_x) 0.5) (+ 2 fin_y)) 0.45 0. ) (textdisplay "N" (list (+ 2 fin_x) (* 0.5 fin_y)) 0.45 0.) (command "_.zoom" "_EXTENT") ) ;;;draw_final_text