Math-PlanePath
view release on metacpan or search on metacpan
examples/c-curve-wx.pl view on Meta::CPAN
#------------------------------------------------------------------------------
# mouse drag
# $drag_x,$drag_y are the X,Y position where dragging started.
# If dragging is not in progress then $drag_x is undef.
my ($drag_x, $drag_y);
# $event is a wxMouseEvent
sub OnLeftDown {
my ($draw, $event) = @_;
### Draw OnLeftDown() ...
$drag_x = $event->GetX;
$drag_y = $event->GetY;
$event->Skip(1); # propagate to other processing
}
sub OnMotion {
my ($draw, $event) = @_;
### Draw OnMotion() ...
if ($event->Dragging) {
if (defined $drag_x) {
### drag ...
my $x = $event->GetX;
my $y = $event->GetY;
$x_offset += $x - $drag_x;
$y_offset += $y - $drag_y;
$drag_x = $x;
$drag_y = $y;
$draw->Refresh;
}
}
}
#------------------------------------------------------------------------------
# drawing
sub TopStart {
my ($k) = @_;
return (2**$k + ($k%2==0 ? -1 : 1))/3;
}
sub TopEnd {
my ($k) = @_;
return TopStart($k+1);
}
sub OnSize {
my ($draw, $event) = @_;
$draw->Refresh;
}
# $idle_drawing is a coderef which is setup by OnPaint() to draw more of the
# curves. If it doesn't finish the drawing then it does a ->RequestMore()
# to go again when next idle (which might be immediately).
my $idle_drawing;
sub OnPaint {
my ($draw, $event) = @_;
### Drawing OnPaint(): $event
### foreground: $draw->GetForegroundColour->GetAsString(4)
### background: $draw->GetBackgroundColour->GetAsString(4)
my $busy = Wx::BusyCursor->new;
my $dc = Wx::PaintDC->new ($draw);
{
my $brush = $dc->GetBackground;
$brush->SetColour ($draw->GetBackgroundColour);
$dc->SetBackground ($brush);
$dc->Clear;
}
# $brush->SetColour (Wx::wxWHITE);
# $brush->SetStyle (Wx::wxSOLID());
# $dc->SetBrush ($brush);
#
# $dc->DrawRectangle (20,20,100,100);
my $colour = Wx::wxGREEN();
{
my $pen = $dc->GetPen;
$pen->SetColour($colour);
$dc->SetPen($pen);
}
my $brush = $dc->GetBrush;
$brush->SetColour ($colour);
$brush->SetStyle (Wx::wxSOLID());
$dc->SetBrush ($brush);
my ($width,$height) = $dc->GetSizeWH;
### $width
### $height
my ($n_lo, $n_hi);
if ($type eq 'Part') {
$n_lo = TopStart($level);
$n_hi = TopEnd($level);
} else {
($n_lo, $n_hi) = $path->level_to_n_range($level);
}
my ($x_lo,$y_lo) = $path->n_to_xy($n_lo);
my ($x_hi,$y_hi) = $path->n_to_xy($n_hi);
my ($dx,$dy) = ($x_hi-$x_lo, $y_hi-$y_lo);
my $len = hypot($dx,$dy);
my $angle = atan2($dy,$dx) * 180 / M_PI(); # dx,dy plus 180deg
### $angle
### $len
my $to01 = Geometry::AffineTransform->new;
$to01->translate(-$x_lo, -$y_lo);
$to01->rotate(- $angle);
if ($len) {
$to01->scale(1/$len, 1/$len);
}
my $t = $types_hash{$type};
### $t
my $min_x = $t->{'min_x'};
my $min_y = $t->{'min_y'};
my $max_x = $t->{'max_x'};
my $max_y = $t->{'max_y'};
( run in 2.142 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )