[Perl] Programm für Funktionsgraphen funktioniert nicht richtig

Flacx

Mitglied
Thread Starter
Dabei seit
14.01.2013
Beiträge
28
Reaktionspunkte
0
Hi.
Momentan habe ich relativ wenig zu tun und habe mir deswegen mal die Idee eines Perl - Programms ausgedacht, mit dessen Hilfe man einfache mathematische Graphen darstellen Kann.
Zum Beispiel: f(x) = 3x + 1
Ich habe dafür das Modul GD eingebunden um eine PNG Datei zu erstellen in der ich mithilfe der line-Funktion des Moduls den Graphen zeichne, was in etwa so funktioniert:
Wenn ich schrittzahl 1 und Funktion f(x) = 5x habe (also eine Einheit pro durchgang) dann ist bei x = 1 gleichzeitig f(x) = 5. Bei x = 2 ist f(x) dann gleich 10 etc...
Nun ist aber die Positionierung innerhalb des Bildes falsch. (Falsche Ecke als Startpunkt). Ich musste das Bild erst einige male drehen und ich hatte den Eindruck es wäre immer noch spiegelverkeht.
Findet jemand den Fehler?
Hier ist der Code: (2 Zeilen sind nur unter Windoof ausführbar, die habe ich auskommentiert)
Code:
#!usr/bin/perl
use GD;
# use Win32;
print "Please choose Size of coordinate system\n(10, 20, 50, 100, 500, 1000)\n";
chomp($size = <STDIN>);
$size2 = $size;        
$graph = new GD::Image($size, $size2);
$white = $graph->colorAllocate(255, 255, 255);
$black = $graph->colorAllocate(0, 0, 0);


print "What multiplicator shall the function have? (z.B.: 5x)\n";
chomp($multi = <STDIN>);
print "Shall Y have a start Value?(0 for no)\n";
chomp($start = <STDIN>);
print "Please enter Stepvalue (even and by 10 dividable)\n";
chomp($step = <STDIN>);
system("cls");
if ($start != 0) {
    print "\nf(x) = $multi", "x + $start\n";
} else {
    print "\nf(x) = $multi", "x\n";
}
print "Stepping forward with $step\nWriting Graph under name Graph.png to Disk.\n";
# Win 
# $caution = Win32::MsgBox("Please make sure no other files exist under this name (Graph.png)!", 0, "Caution!");
#
open (OP, ">Graph.png");


$stepcounter = 0;
$x1 = 0;
if ($start != 0) {
    $y1 = $start;
} else { $y1 = 0; }


$x2 = 0;
$y2 = 0;
$x3 = 0;
$y3 = 0;
$round = 1;


while ($stepcounter != $size2) { #von links nach rechts
    
    $up = $step*$multi;
    if ($round == 1) {
        $x2 += $step;
        $y2 += $up;
        $graph->line($x1, $y1, $x2, $y2, $black);
    } elsif ($round == 2) {
        $x3 = $x2 + $step;
        $y3 = $y2 + $up;
        $graph->line($x2, $y2, $x3, $y3, $black);
    } elsif ($round == 3) {
        $x1 = $x3 + $step;
        $y1 = $y3 + $up;
        $graph->line($x3, $y3, $x1, $y1, $black);
    }
    $round++;
    if ($round == 4) { $round = 1; }
    $stepcounter += $step;
}


$pngd = $graph->png;
binmode OP;
print OP $pngd;
close OP;
exit;

Danke schonmal ;)

P.S: Dekoration wie Beschriftung und co kommt noch. Ist die allererste Version.
 
Ohne jetzt deinen Code durchgesehen zu haben: Wo liegt in Perl der Bezugspunkt? Nicht unüblich ist ja links oben und ansteigen tuts nach rechts bzw. unten. Da ist dann Koordinatentransformation gefragt.
In Cocoa ist der Bezugspunkt links unten und ansteigen tuts nach rechts bzw. oben, also so wie man es eigentlich aus Mathe und Co gewohnt ist. Um in der Programmierwelt wieder gewohnt arbeiten zu können gibts da auch extra ne Funktion die das Koordinatensystem in Cocoa umwirft mit Namen isFlipped ;)
 
Oben Links:

http://search.cpan.org/~lds/GD-1.38/GD.pm#Drawing_Commands schrieb:
Drawing Commands

These methods allow you to draw lines, rectangles, and elipses, as well as to perform various special operations like flood-fill.

$image->setPixel($x,$y,$color)

This sets the pixel at (x,y) to the specified color index. No value is returned from this method. The coordinate system starts at the upper left at (0,0) and gets larger as you go down and to the right.
[..]
 
ah ok, Danke. Ich dachte, dass es quasi eine Norm ist bei Bildinformationen und Koordinatensystem unten links anzufangen.
:)
 
Zurück
Oben Unten