File: Edge.pm

package info (click to toggle)
algotutor 0.8.6-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 580 kB
  • sloc: perl: 2,563; makefile: 41; php: 24; sh: 1
file content (156 lines) | stat: -rw-r--r-- 4,419 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# Author:  Chao-Kuei Hung
# For more info, including license, please see doc/index.html

package Edge;
# Edge of a Graph

use strict;
use Carp;
use vars qw(@ISA);
@ISA = qw(Configurable);

use Configurable;
use Vector2;
use overload
    '""' => 'stringify',
    'fallback' => 1
#    'eq' => '()',
#    'fallback' => undef
;

sub new {
    my ($class, $src, $tgt, %opts) = @_;
    $class = ref($class) if ref($class);
    croak "$src is not a Vertex" unless $src->isa("Vertex");
    croak "$tgt is not a Vertex" unless $tgt->isa("Vertex");
    my ($self) = bless ::deep_copy(\%opts), $class;
    $self->{"#host"} = $src->host();
    my ($cv) = $self->host()->cget(-canvas);
    $self->{shape_id} = $cv->createLine(0, 0, 0, 0);
    $self->{text_id} = $cv->createText(0, 0);
#    $self->{canvas}->addtag($name, "withtag", $self->{obj}{$name}{shape_id});
    $self->set_ends($src, $tgt);
    $self->{-arrow} = $self->cget(-directed) ? "last" : "none";
    $self->configure($self->get_all_opts());
    return $self;
}

sub stringify {
    # serves to identify an edge, such as key for hash
    my ($self) = @_;
    return "$self->{adj}{src}-$self->{adj}{tgt}";
}

sub destroy {
    my ($self) = @_;
    $self->host()->cget(-canvas)->delete(@{$self}{"shape_id","text_id"});
}

sub source {
    my ($self, $nv) = @_;
    croak "you probably wanted to call set_ends()?" if $#_ >= 1;
    return $self->{adj}{src};
}

sub target {
    my ($self, $nv) = @_;
    croak "you probably wanted to call set_ends()?" if $#_ >= 1;
    return $self->{adj}{tgt};
}

sub host {
    return $_[0]->{"#host"};
}

# intersection of ellipse (x/a)^2 + (y/b)^2 = 1 with line x/dx = y/dy
sub _x_oval_ {
    my ($dx, $dy, $a, $b) = @_;
    $a = $dx / $a;
    $b = $dy / $b;
    my ($t) = sqrt($a*$a + $b*$b);
    return Vector2->new($dx/$t, $dy/$t);
}

sub set_ends {
    my ($self, $src, $tgt, %opts) = @_;
    my ($cv) = $self->host()->cget(-canvas);
    @{ $self->{adj} }{qw(src tgt)} = ($src, $tgt);
    my ($pos_s, $size_s) = $src->_get_cv_geom_();
    my ($pos_t, $size_t) = $tgt->_get_cv_geom_();
    $size_s = $size_s->pw_div(2);
    $size_t = $size_t->pw_div(2);
    my ($d) = $pos_t - $pos_s;
    if ($d->norm() > 1) {
	$pos_s += _x_oval_( @$d, @$size_s);
	$pos_t -= _x_oval_( @$d, @$size_t);
    } else {
	carp "Both ends of edge $self coincide (at $pos_s)\n"
	    unless $self->cget(-quiet);
    }
    # now turn $d 90 degrees counter-clockwise and make it a unit vector
    @$d = (-$d->[1], $d->[0]);
    $d = $d->pw_div($d->norm());
    my ($s, $t);
    $s = $d->pw_mul($self->cget(-directed) ? 4 : 0);
    $pos_s += $s; $pos_t += $s;
    $cv->coords($self->{shape_id}, @$pos_s, @$pos_t);
    $s += $d->pw_mul(8);
    $s = $s->pw_mul(-1) if (not $self->cget(-directed) and
	$s->[0] < 0 or $s->[0] == 0 and $s->[1] < 0);
    $t = $self->cget(-directed) ? 2/5 : 1/2;
    $cv->coords($self->{text_id},
	@{ $pos_s->pw_mul($t) + $pos_t->pw_mul(1-$t) + $s }
    );
}

sub configure {
    my ($self, %opts) = @_;
    my ($k, %shape_opts, %text_opts);
    my ($opt_map) = {
	-weight   => [undef, undef],
	-text    => [undef, "-text"],
	-width	 => ["-width", undef],
	-fill    => [undef, undef],
	-outline => ["-fill", "-fill"],
	-thick   => ["-width", undef],
	-arrow   => ["-arrow", undef],
	-stipple => [undef, undef],
	-outlinestipple => ["-stipple", undef],
	-state   => ["-state", "-state"],
    };
    if (exists $opts{-directed}) {
	$self->{-directed} = delete $opts{-directed};
	$self->set_ends($self->source(), $self->target());
    }
    if (exists $opts{-status}) {
	$self->{-status} = delete $opts{-status};
	my ($ha) = $self->host()->cget(-appearance);
	carp "unknown status $self->{-status} ignored"
	    unless exists $ha->{$self->{-status}};
	%opts = (%{ $ha->{$self->{-status}} }, %opts);
    }
    foreach $k (keys %opts) {
	carp "unknown option $k ignored" unless exists($opt_map->{$k});
	$shape_opts{ $opt_map->{$k}[0] } = $opts{$k}
	    if defined $opt_map->{$k}[0];
	$text_opts{ $opt_map->{$k}[1] } = $opts{$k}
	    if defined $opt_map->{$k}[1];
    }
    my ($cv) = $self->host()->cget(-canvas);
    $cv->itemconfigure($self->{shape_id}, %shape_opts);
    $cv->itemconfigure($self->{text_id}, %text_opts);
}

sub get_all_opts {
    my ($self) = @_;
    my (%opts) = $self->SUPER::get_all_opts();
    delete @opts{qw(-display -shape -size -name)};
    return %opts;
}

$::Config->{Edge} = {
    -status => "init",
};

1;