kokoko12000
Programmer
Hi all,
I have 2 draw 20x20 grid of points and also a circle. I have done both of them. Along with that I have to find what grid points are overlapping with the edge of the circle, and change their color. But I am not able to know how to find the overlapping grid points with the edge of the circle. I found that I have to use find overlapping. Since I only went thru the tutorials yesterday, Im unable to use that. I dunno whether this is the right way also. Since I have created ovals as grid points, I dunno how to set tags or set ids dynamically to them. I am attaching my code, pls look into it and give me suggestions.
# Set window title
wm title . att2
set cid 0
#Create a Frame
frame .f1 -borderwidth 4
pack .f1 -side top
#Select width and height of the canvas, making it fixed
set cht 400
set cwt $cht
#Create a canvas and set it in the frame
canvas .f1.c1 -width $cwt -height $cht
# Drawing the ovals
set pix [expr {$cwt/20}]
#Put the points in
for {set i 0} {$i<$cwt} {incr i $pix} {
set x2 [expr {$i+$pix}]
for {set j 0} {$j<$cwt} {incr j $pix} {
set y2 [expr {$j+$pix}]
set midx [expr {($i+$x2)/2}]
set midy [expr {($j+$y2)/2}]
.f1.c1 create oval [expr {$midx-5}] [expr {$midy-5}] [expr {$midx+5}] [expr {$midy+5}] -fill grey
}
}
bind .f1.c1 <ButtonPress-1> {button_pressed %x %y}
bind .f1.c1 <B1-Motion> {button_in_motion %x %y}
bind .f1.c1 <ButtonRelease-1> {button_release %x %y}
proc button_pressed {x y} {
global x0 y0
set x0 $x
set y0 $y
}
proc button_in_motion {x y} {
global x0 y0 cid x1 y1 x2 y2
.f1.c1 delete $cid
set x2 $x
set y2 $y
set radius [expr hypot(($x2 - $x0),($y2 - $y0))]
set x1 [expr $x0 - $radius]
set y1 [expr $y0 - $radius]
set x2 [expr $x0 + $radius]
set y2 [expr $y0 + $radius]
set cid [.f1.c1 create oval $x1 $y1 $x2 $y2 -outline white]
}
proc button_release {x y} {
global x0 y0 cid x1 y1 x2 y2
set x2 $x
set y2 $y
set radius [expr hypot(($x2 - $x0),($y2 - $y0))]
set x1 [expr $x0 - $radius]
set y1 [expr $y0 - $radius]
set x2 [expr $x0 + $radius]
set y2 [expr $y0 + $radius]
.f1.c1 delete $cid
set cid [.f1.c1 create oval $x1 $y1 $x2 $y2 -outline blue]
}
pack .f1.c1 -side bottom
I have 2 draw 20x20 grid of points and also a circle. I have done both of them. Along with that I have to find what grid points are overlapping with the edge of the circle, and change their color. But I am not able to know how to find the overlapping grid points with the edge of the circle. I found that I have to use find overlapping. Since I only went thru the tutorials yesterday, Im unable to use that. I dunno whether this is the right way also. Since I have created ovals as grid points, I dunno how to set tags or set ids dynamically to them. I am attaching my code, pls look into it and give me suggestions.
# Set window title
wm title . att2
set cid 0
#Create a Frame
frame .f1 -borderwidth 4
pack .f1 -side top
#Select width and height of the canvas, making it fixed
set cht 400
set cwt $cht
#Create a canvas and set it in the frame
canvas .f1.c1 -width $cwt -height $cht
# Drawing the ovals
set pix [expr {$cwt/20}]
#Put the points in
for {set i 0} {$i<$cwt} {incr i $pix} {
set x2 [expr {$i+$pix}]
for {set j 0} {$j<$cwt} {incr j $pix} {
set y2 [expr {$j+$pix}]
set midx [expr {($i+$x2)/2}]
set midy [expr {($j+$y2)/2}]
.f1.c1 create oval [expr {$midx-5}] [expr {$midy-5}] [expr {$midx+5}] [expr {$midy+5}] -fill grey
}
}
bind .f1.c1 <ButtonPress-1> {button_pressed %x %y}
bind .f1.c1 <B1-Motion> {button_in_motion %x %y}
bind .f1.c1 <ButtonRelease-1> {button_release %x %y}
proc button_pressed {x y} {
global x0 y0
set x0 $x
set y0 $y
}
proc button_in_motion {x y} {
global x0 y0 cid x1 y1 x2 y2
.f1.c1 delete $cid
set x2 $x
set y2 $y
set radius [expr hypot(($x2 - $x0),($y2 - $y0))]
set x1 [expr $x0 - $radius]
set y1 [expr $y0 - $radius]
set x2 [expr $x0 + $radius]
set y2 [expr $y0 + $radius]
set cid [.f1.c1 create oval $x1 $y1 $x2 $y2 -outline white]
}
proc button_release {x y} {
global x0 y0 cid x1 y1 x2 y2
set x2 $x
set y2 $y
set radius [expr hypot(($x2 - $x0),($y2 - $y0))]
set x1 [expr $x0 - $radius]
set y1 [expr $y0 - $radius]
set x2 [expr $x0 + $radius]
set y2 [expr $y0 + $radius]
.f1.c1 delete $cid
set cid [.f1.c1 create oval $x1 $y1 $x2 $y2 -outline blue]
}
pack .f1.c1 -side bottom