line in piece

17 views
Skip to first unread message

swan lee

unread,
Feb 1, 2011, 12:13:08 AM2/1/11
to
hello,
how would it be possible to have a in a canvas a line that can be cut in
piece at the place i click with the mouse?
. .
/ \ / \ /
/ ° \ /
/ °

when i click on the line, it create a point that can be scroll up/down
to modify the line.
like they do for sound editor for example.

any ideas about that?
thanks
++

Arjen Markus

unread,
Feb 1, 2011, 2:56:47 AM2/1/11
to

Sure, that is quite possible!

You will need to bind to several events though and it is not
completely
trivial, but if you look at the standard examples that come with Tk,
you
will find one where you can edit an xy-graph with dots.

The way to split up the line is:

set oldCoords [.c coords $line] ;# line holds the ID of the line
set newCoords [insertNewPoint $oldCoords $x $y] ;# Not entirely
trivial
;# - where should you
insert the new point?
.c coords $line $newCoords

Well, something along those lines

Regards,

Arjen

swan lee

unread,
Feb 1, 2011, 5:47:29 AM2/1/11
to
Le 01/02/11 08:56, Arjen Markus a �crit :

i've started like that, but i don't know how to use array in Tcl to
maintain several lines coordinates, because i think that each time i put
a point, i need to short the first line and create another one...

frame .f
canvas .f.c -width 100 -height 100
.f.c create line 0 0 100 100 -tags line0
bind .f.c <ButtonPress-1> {makePoint %W %x %y}
bind .f.c <B1-Motion> {thisPointMove %W %x %y}

pack .f .f.c -side top

set point(count) 0


proc makePoint {win p_x p_y} {
upvar #0 point v

set x [$win canvasx $p_x]
set y [$win canvasy $p_y]
set eline [$win gettags [$win find closest $x $y]]

if {[string equal -length 4 [string map {" current" ""} $eline] "line"]} {
$win create oval [expr {$x-4}] [expr {$y-4}] [expr {$x+4}] [expr
{$y+4}] -tags point$v(count) -fill red
incr v(count) 1
}
}

proc thisPointMove {win p_x p_y} {
upvar #0 point v

set x [$win canvasx $p_x]
set y [$win canvasy $p_y]
set point [string map {" current" ""} [$win gettags [$win find closest
$x $y 1]]]

if {[string equal -length 5 $point "point"]} {
$win coords $point [expr {$x-4}] [expr {$y-4}] [expr {$x+4}] [expr {$y+4}]
}
}

>
> Regards,
>
> Arjen

Arjen Markus

unread,
Feb 1, 2011, 7:01:21 AM2/1/11
to
> > Arjen- Tekst uit oorspronkelijk bericht niet weergeven -
>
> - Tekst uit oorspronkelijk bericht weergeven -- Tekst uit oorspronkelijk bericht niet weergeven -
>
> - Tekst uit oorspronkelijk bericht weergeven -

The coordinates are returned as a list. No need to mess about with
arrays :).

Your code looks a bit too complicated - you can use the move
subcommand to
move one or more objects around.

Regards,

Arjen

Gerald W. Lester

unread,
Feb 1, 2011, 8:53:42 AM2/1/11
to
You might want to take a look at the "Simple 2D plot" demo that comes as
part of the widget demo. It is a scatter plot, but should be simple to
change to a line plot.

> The coordinates are returned as a list. No need to mess about with
> arrays :).
>
> Your code looks a bit too complicated - you can use the move
> subcommand to
> move one or more objects around.

--
+------------------------------------------------------------------------+
| Gerald W. Lester, President, KNG Consulting LLC |
| Email: Gerald...@kng-consulting.net |
+------------------------------------------------------------------------+

swan lee

unread,
Feb 1, 2011, 9:25:10 AM2/1/11
to
Le 01/02/11 14:53, Gerald W. Lester a �crit :

> You might want to take a look at the "Simple 2D plot" demo that comes as
> part of the widget demo. It is a scatter plot, but should be simple to
> change to a line plot.
>

Gerald,
thank you for pointing this demo, i added the selected tags to current
round in order to move it.
now, any suggestion to cut the line and maintain geometry in an array
(or somewhere...)

++

Arjen Markus

unread,
Feb 1, 2011, 9:38:59 AM2/1/11
to

I do have a few, but no time at the moment to work that out, which is
a pity.

Regards,

Arjen

Uwe Klein

unread,
Feb 1, 2011, 10:07:06 AM2/1/11
to
Well one solution is to find the segment indices of the element clicked on
and insert the current x/y set at that position.

The other is to draw the segmented line as a succesion of two point lines
and maybe add some kind of tick at each common point.

uwe


tomas

unread,
Feb 14, 2011, 2:01:21 AM2/14/11
to

Hi,

swan lee <sl12...@free.fr> writes:

> hello,
> how would it be possible to have a in a canvas a line that can be cut in
> piece at the place i click with the mouse?

maybe too late for you (I don't know), but I was inspired by your
question and put together a small example, which you will find here:

<http://wiki.tcl.tk/11031>

Enjoy
-- tomás

swan lee

unread,
Feb 14, 2011, 2:52:02 AM2/14/11
to
Le 14/02/11 08:01, tomas a écrit :

Thomas,
that's very nice!!
it's not too late for me as it's something i'll add to my app in the future.

many thanks for sharing your work!
best regards

Reply all
Reply to author
Forward
0 new messages