//// Testpunkte
// 3 Koordinatenpunkte des Dreiecks
$data=array();
$data[0]['X']=4;
$data[0]['Y']=12;
$data[1]['X']=50;
$data[1]['Y']=50;
$data[2]['X']=35;
$data[2]['Y']=25;
/*
// 3 Koordinatenpunkte des Dreiecks
$data=array();
$data[0]['X']=17;
$data[0]['Y']=28;
$data[1]['X']=3;
$data[1]['Y']=10;
$data[2]['X']=35;
$data[2]['Y']=15;*/
/*
// 3 Koordinatenpunkte des Dreiecks
$data=array();
$data[0]['X']=34;
$data[0]['Y']=76;
$data[1]['X']=53;
$data[1]['Y']=7;
$data[2]['X']=21;
$data[2]['Y']=95;*/
// Sortieren der Punkte nach kleinsten XWert bis maximal X-Wert
$sortArray = array();
foreach($data as $key => $array) {
$sortArray[$key] = $array[0];
}
array_multisort($sortArray, SORT_ASC, SORT_NUMERIC, $data);
// Sortieren so das Punkt 3 grössten YWert stellt
foreach($data as $key => $array) {
if ($key==1) {
$array1=$array;
}
if ($key==2 AND $array1['Y']>$array['Y']) {
$data[1]['X']=$array['X'];
$data[1]['Y']=$array['Y'];
$data[2]['X']=$array1['X'];
$data[2]['Y']=$array1['Y'];
}
}
//// Rasterung von Linien nach Bresenham-Algorithmus
// Für jede Seite des Dreiecks Arrays mit allen darauf liegenden Punkten berechnen/speichern
// Jede Seite ist eine Linie mit x Punkten
//// Rasterung von Linien nach Bresenham-Algorithmus
// Linie1 - Bresenham-Algorithmus | diese Linie stellt Preferenz und Rechnungsbezugslinie dar (Minimalster Punkt1 X-Wert zu Punkt3 maximaler Y-Wert)
// Die Schleife durchläuft zuerst die X-Achse und innerhalb dann die Y-Achse
$x0=$data[0]['X']; // Punkt mit kleinsten X-Wert
$y0=$data[0]['Y'];
$x1=$data[2]['X'];
$y1=$data[2]['Y']; // Punkt mit grössten Y-Wert
$dx = abs($x1 - $x0);
$sx = $x0 < $x1 ? 1 : -1;
$dy = abs($y1 - $y0);
$sy = $y0 < $y1 ? 1 : -1;
$err = ($dx>$dy ? $dx : -$dy)/2;
// echo "<p>Linie1</p>";
while (true) {
// Optional: Linie ausgeben:
// echo "<p>$x0/$y0</p>";
$Line1['X'][]=$x0;
$Line1['Y'][]=$y0;
if ($x0 === $x1 && $y0 === $y1) break;
$e2 = $err;
if ($e2 > -$dx) { $err -= $dy; $x0 += $sx; }
if ($e2 < $dy) { $err += $dx; $y0 += $sy; }
}
// Linie2 - Bresenham-Algorithmus
if ($data[1]['X']<$data[2]['X']) {
$x0=$data[1]['X'];
$y0=$data[1]['Y'];
$x1=$data[2]['X'];
$y1=$data[2]['Y'];
}
else {
$x1=$data[1]['X'];
$y1=$data[1]['Y'];
$x0=$data[2]['X'];
$y0=$data[2]['Y'];
}
$dx = abs($x1 - $x0);
$sx = $x0 < $x1 ? 1 : -1;
$dy = abs($y1 - $y0);
$sy = $y0 < $y1 ? 1 : -1;
$err = ($dx>$dy ? $dx : -$dy)/2;
//echo "<p>Linie2</p>";
while (true) {
// Optional: Linie ausgeben:
//echo "<p>$x0/$y0</p>";
$Line2['X'][]=$x0;
$Line2['Y'][]=$y0;
if ($x0 === $x1 && $y0 === $y1) break;
$e2 = $err;
if ($e2 > -$dx) { $err -= $dy; $x0 += $sx; }
if ($e2 < $dy) { $err += $dx; $y0 += $sy; }
}
// Linie3 - Bresenham-Algorithmus
$x0=$data[0]['X'];
$y0=$data[0]['Y'];
$x1=$data[1]['X'];
$y1=$data[1]['Y'];
$dx = abs($x1 - $x0);
$sx = $x0 < $x1 ? 1 : -1;
$dy = abs($y1 - $y0);
$sy = $y0 < $y1 ? 1 : -1;
$err = ($dx>$dy ? $dx : -$dy)/2;
// echo "<p>Linie3</p>";
while (true) {
// Optional: Linie ausgeben:
// echo "<p>$x0/$y0</p>";
$Line3['X'][]=$x0;
$Line3['Y'][]=$y0;
if ($x0 === $x1 && $y0 === $y1) break;
$e2 = $err;
if ($e2 > -$dx) { $err -= $dy; $x0 += $sx; }
if ($e2 < $dy) { $err += $dx; $y0 += $sy; }
}
// Maximal X-Wert ermitteln für SchleifenENDE
// Die Schleife durchläuft zuerst die X-Achse und innerhalb dann die Y-Achse
$Xmaximum=max($data[0]['X'],$data[1]['X'],$data[2]['X']);
$Xminimum=min($data[0]['X'],$data[1]['X'],$data[2]['X']);
///////////////
// Dreiecksfläche: Alle Koordinatenpaare innerhalb des Dreiecks in Array $Dreiecksfläche speichern
///////////////
// Schleife durchläuft X-Achse bis $Xmaximum Dies ist immer der maximale X-Achsenwert durch obrige Sortierung
for($x=$Xminimum; $x<=$Xmaximum; $x++){
// YAchse-Yminimuma und YAchse-Ende festlegen
// Solange Linie3 und Linie1 auf der X-Achse liegen
if (end($Line3['X'])>=$x AND end($Line1['X'])>=$x) {
$Keys=array_keys($Line3['X'], $x);
$Key=end($Keys);
$Yminimum=$Line3['Y'][$Key];
$Keys=array_keys($Line1['X'], $x);
$Key=$Keys[0];
$Ymaximum=$Line1['Y'][$Key];
}
// WENN Grundlinie(Linie3) kürzer als Oberlinie(Linie1)
if (end($Line3['X'])<$x) {
//if (!isset($Line3['Y'][$x-$Xminimum])) {
$Keys=array_keys($Line2['X'], $x);
$Key=$Keys[0];
$Yminimum=$Line2['Y'][$Key];
$Keys=array_keys($Line1['X'], $x);
$Key=end($Keys);
$Ymaximum=$Line1['Y'][$Key];
}
// WENN Grundlinie länger als Oberlinie
if (end($Line1['X'])<$x) {
$Keys=array_keys($Line2['X'], $x);
$Key=$Keys[0];
$Ymaximum=$Line2['Y'][$Key];
$Keys=array_keys($Line3['X'], $x);
$Key=end($Keys);
$Yminimum=$Line3['Y'][$Key];
}
// Schleife Y-Achse
for($y=$Yminimum; $y<=$Ymaximum; $y++){
$Dreiecksfläche['X'][]=$x;
$Dreiecksfläche['Y'][]=$y;
// Optional: Dreiecksfläche ausgeben:
// echo "<p>$x/$y</p>";
}
// Trenn-Linie (Debugging)
// echo "<p>Next Line $x</p>";
}
//// Dreiecksfläche zeichnen (PHP GD-Lib)
// Linien-Legende
echo "<p>rot = Linie 1</p>";
echo "<p>grün = Linie 2</p>";
echo "<p>blau = Linie 3</p>";
$Zooming=15; // Zoomfaktor - zeigt die Linien genauer (Debugging)
$BILD = imageCreate(100*$Zooming,100*$Zooming); // Bild erstellt, Bild absolut leer
imageColorAllocate($BILD,0,0,0); // erstes Allocate -> der Hintergrund des Bildes wird Schwarz sein
// Linie 1 zeichnen
$rot = imageColorAllocate($BILD,255,0,0);
for($i=0; $i<=count($Line1['X']); $i++){
//echo "<p>{$Line1['X'][$i]},{$Line1['Y'][$i]}</p>";
if ($Line1['X'][$i+1] AND $Line1['Y'][$i+1]) {
imageLine($BILD,$Line1['X'][$i]*$Zooming,$Line1['Y'][$i]*$Zooming,$Line1['X'][$i+1]*$Zooming,$Line1['Y'][$i+1]*$Zooming,$rot);
}
}
// Linie 2 zeichnen
$grün = imageColorAllocate($BILD,0,255,0);
for($i=0; $i<=count($Line2['X']); $i++){
//echo "<p>{$Line2['X'][$i]},{$Line2['Y'][$i]}</p>";
if ($Line2['X'][$i+1] AND $Line2['Y'][$i+1]) {
imageLine($BILD,$Line2['X'][$i]*$Zooming,$Line2['Y'][$i]*$Zooming,$Line2['X'][$i+1]*$Zooming,$Line2['Y'][$i+1]*$Zooming,$grün);
}
}
// Linie 3 zeichnen
$blau = imageColorAllocate($BILD,0,0,255);
for($i=0; $i<=count($Line3['X']); $i++){
//echo "<p>{$Line3['X'][$i]},{$Line3['Y'][$i]}</p>";
if ($Line3['X'][$i+1] AND $Line3['Y'][$i+1]) {
imageLine($BILD,$Line3['X'][$i]*$Zooming,$Line3['Y'][$i]*$Zooming,$Line3['X'][$i+1]*$Zooming,$Line3['Y'][$i+1]*$Zooming,$blau);
}
}
// Dreiecksfläche
$gelb = imageColorAllocate($BILD,255,255,0); // rot: 255, grün: 255, blau: 0 --> gelb, siehe Farbenlehre
for($i=0; $i<=count($Dreiecksfläche['X']); $i++){
//echo "<p>{$Dreiecksfläche['X'][$i]}/{$Dreiecksfläche['Y'][$i]}</p>";
// Schräge Linien aussparen in der If-Bedingung: AND $Dreiecksfläche['X'][$i]==$Dreiecksfläche['X'][$i+1]
if ($Dreiecksfläche['X'][$i+1] AND $Dreiecksfläche['Y'][$i+1] AND $Dreiecksfläche['X'][$i]==$Dreiecksfläche['X'][$i+1]) {
imageLine($BILD,$Dreiecksfläche['X'][$i]*$Zooming,$Dreiecksfläche['Y'][$i]*$Zooming,$Dreiecksfläche['X'][$i+1]*$Zooming,$Dreiecksfläche['Y'][$i+1]*$Zooming,$gelb);
// Optional: Dreiecksfläche ausgeben:
//echo "<p>{$Dreiecksfläche['X'][$i+1]}/{$Dreiecksfläche['Y'][$i+1]}</p>";
}
}
// Bild speichern
imageJPEG($BILD,"img/triangle.jpg",100);
// Bild ausgeben
echo "<img src=\"img/triangle.jpg\">";