Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Finding overlapping items (Help needed badly)

Status
Not open for further replies.

kokoko12000

Programmer
Apr 14, 2003
13
0
0
US
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

 
Exec, drag and see.
Code:
  pack [canvas .c -height 200 -width 200]
  for {set i 0} {$i < 10} {incr i}   {
    for {set j 0} {$j < 10} {incr j}     {
      set x1 [expr {$j * 20}]
      set y1 [expr {$i * 20}]
      set x2 [expr {$x1 + 20}]
      set y2 [expr {$y1 + 20}]
      .c create rectangle $x1 $y1 $x2 $y2 -fill gray -tags [list rect$i-$j rects]
    }
  }
  .c bind rects <ButtonPress-1> {clic %x %y}
  .c bind rects <Motion> {move %x %y}
  .c bind rects <ButtonRelease-1> {release}
  set ::drawing 0
  proc clic {x y}   { 
    set ::drawing 1
    set ::xc $x; set ::yc $y 
    set ::circle [.c create oval $::xc $::yc $::xc $::yc]
  }
  proc move {x y}   {
    if {!$::drawing} { return }
    set radius [expr {sqrt(pow($::xc - $x,2) + pow($::yc - $y,2))}]
    set x1 [expr {$::xc - $radius}]
    set y1 [expr {$::yc - $radius}]
    set x2 [expr {$::xc + $radius}]
    set y2 [expr {$::yc + $radius}]
    .c coords $::circle $x1 $y1 $x2 $y2
  }
  proc release {}   {
    set ::drawing 0
    .c itemconfig rects -fill gray
    for {set i 0} {$i < 10} {incr i}     {
      for {set j 0} {$j < 10} {incr j}       {
        set x1 [expr {$j * 20}]
        set y1 [expr {$i * 20}]
        set x2 [expr {$x1 + 20}]
        set y2 [expr {$y1 + 20}]
        set indexes [.c find overlapping $x1 $y1 $x2 $y2]
        if {[lsearch $indexes $::circle] != -1} { .c itemconfig rect$i-$j -fill blue }
      }
    }
  }
HTH

ulis
 
Ulis,
Thanks for the reply. Its working. Great dude!!!

-Kiran.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top