Veröffentlicht 30. April 201411 j Hallo Leute, bin hier relativ neu und habe mal eine Frage an euch. Ich bin derzeit noch in der Ausbildung zum Fachinformatiker Fachrichtung Anwendungsentwicklung und habe eine kleine Aufgabe innerhalb des Betriebes bekommen. Nun bin ich noch relativ frisch dabei (PHP, etc) und finde den richtigen Algorithmus nicht. Vielleicht kann mir ja jemand helfen Aufgabenstellung: Erschaffe eine Karte (php) mit 8x8 quadratischen Feldern. Die Felder sollen über eine Buchstaben-Zahl Kombination ansprechbar sein. (Bis hierhin easy) Berechne nun Diagonal die Entfernung vom Mittelpunkt von Planquadrat C3 zu F7. Generalisiere die Methode, sodass du beliebige Felder nutzen kannst. Nächster Schritt: Die Bewegungen über Felder können nur an Kanten erfolgen. Diagonales ziehen ist daher nicht möglich. Finde einen Algorithmus, der einen Weg von B2 zu D3 findet. Generalisiere die Formel. Ich erwarte keine kompletten Lösungen von euch . Nur ein wenig Hilfestellung wäre nett . Möchte ja etwas dabei lernen. Mein derzeitige Code: class map { function calc($from, $to, $dia=null) { if($dia == true) { //Diagonal echo "true"; }elseif($dia == false) { //Nicht Diagonal echo "false"; } } function x_axis() { $data = array( "1" => "", "2" => "", "3" => "", "4" => "", "5" => "", "6" => "", "7" => "", "8" => "" ); return $data; } function y_axis() { $data = array( "A" => "", "B" => "", "C" => "", "D" => "", "E" => "", "F" => "", "G" => "", "H" => "" ); return $data; } function view() { $datax = $this->x_axis(); $datay = $this->y_axis(); echo "<table style='border: 1px solid #fff; font-size: 40px; margin: 0 auto;' cellspacing='0'>"; foreach($datax as $keyx => $x) { echo "<tr style='border: 1px solid #fff;'>"; foreach($datay as $keyy => $y) { echo "<td style='border: 1px solid #fff;'>".$keyy.$keyx."</td>"; } echo "</tr>"; } echo "</table>"; } } $mapClass = new map; $mapClass->view(); ?> <table> <form method="POST" action=""> <tr><td>von:</td><td><input type="text" name="from" placeholder="From"></td></tr> <tr><td>zu:</td><td><input type="text" name="to" placeholder="To"></td></tr> <tr><td>Diagonales ziehen:</td><td>Ja<input type="radio" name="dia" value="1">Nein<input type="radio" name="dia" value="0" checked></td></tr> <tr><td></td><td><input type="submit" value="Berechnen" name="calc"></td></tr> </form> </table> <?php if(isset($_POST['calc'])) { $mapClass->calc($_POST['from'], $_POST['to'], $_POST['dia']); } ?> [/PHP] Stecke also bei dem Algo für die Berechnung fest :confused:. Denke die Richtung ,,Satz des Pythagoras" wäre da richtig oder...
30. April 201411 j Hi socressor, der Satz des Pythagoras geht genau in die richtig Richtung. Ich geb dir mal folgenden Hinweis, damit solltest du dann Aufgabenteil 2 und 3 lösen (x1/y1) nach (x2/y2) (x2-x1)^2 + (y2-y1)^2 = z^2 LG
7. Mai 201411 j Sei a der horizontale Abstand von Start- und Zielfeld. Sei b der vertikale Abstand von Start- und Zielfeld. Gesucht ist der "geradlinige" Abstand, nennen wir den mal c. Nach dem Satz des Pythagoras gilt a² + b² = c². Also ist c = √(a² + b²)
7. Mai 201411 j Autor Danke abermals, aber ich meine programmatisch (PHP) Mein derzeitiger Stand: <html> <head> <title>Möp-Karte</title> <style type="text/css"> body { background: #000; color: #fff; } </style> </head> <body> <div align="center"> <h1>Möp-Karte</h1> <?php class map { function calc($from, $to) { $numeric=array($from => '', $to => ''); $letters=array($from => '', $to => ''); $splitter = str_split($from); $splitter2 = str_split($to); foreach($splitter as $fv){ if(is_numeric($fv)) { $numeric[$from]=$numeric[$from].$fv; }else{ $letters[$from]=$letters[$from].$fv; } } foreach($splitter2 as $tv){ if(is_numeric($tv)) { $numeric[$to]=$numeric[$to].$tv; }else{ $letters[$to]=$letters[$to].$tv; } } // var_dump($numeric); // var_dump($letters); //$this->view($numeric,$letters); //$this->view($from,$to); return array($numeric, $letters); } function x_axis($m=null, $n=null) { $data = array( "1" => "", "2" => "", "3" => "", "4" => "", "5" => "", "6" => "", "7" => "", "8" => "" ); if($m != null && $n != null) { $all = $this->calc($m, $n); //var_dump($all); foreach($all[0] as $signed) { if(stristr($m,$signed)) { $data[$signed] = $m; } if(stristr($n,$signed)) { $data[$signed] = $n; } } } return $data; } function y_axis($m=null, $n=null) { $data = array( "A" => "", "B" => "", "C" => "", "D" => "", "E" => "", "F" => "", "G" => "", "H" => "" ); if($m != null && $n != null) { $all = $this->calc($m, $n); //var_dump($all); foreach($all[1] as $signed) { if(stristr($m,$signed)) { $data[$signed] = $m; } if(stristr($n,$signed)) { $data[$signed] = $n; } } } return $data; } function view($from=null, $to=null) { $datax = $this->x_axis($from, $to); $datay = $this->y_axis($from, $to); //var_dump($datax); echo "<table style='border: 1px solid #fff; font-size: 40px; margin: 0 auto;' cellspacing='0'>"; foreach($datax as $keyx => $x) { echo "<tr style='border: 1px solid #fff;'>"; $datax[$keyx]=""; foreach($datay as $keyy => $y) { if($y == $keyy.$keyx) { echo "<td style='border: 1px solid red;'>".$keyy.$keyx."</td>"; } else { echo "<td style='border: 1px solid #fff;'>".$keyy.$keyx."</td>"; } } echo "</tr>"; } echo "</table>"; } } $mapClass = new map; ?> <table> <form method="POST" action=""> <tr><td>von:</td><td><input type="text" name="from" placeholder="From"></td></tr> <tr><td>zu:</td><td><input type="text" name="to" placeholder="To"></td></tr> <tr><td></td><td><input type="submit" value="Berechnen" name="calc"></td></tr> </form> </table> <?php if(isset($_POST['calc'])) { $mapClass->view($_POST['from'], $_POST['to']); }else{ $mapClass->view(); } ?> </div> </body> </html> [/PHP] Das sieht so aus und makiert derzeit Start und Ziel. Bearbeitet 7. Mai 201411 j von socressor Neuster Stand
7. Mai 201411 j Danke abermals, aber ich meine programmatisch (PHP) Das kannst du doch praktisch 1 zu 1 in Code umsetzen. Die Mathematik haben wir dir schon komplett abgenommen. Ein wenig Eigenleistung darf's schon sein.
7. Mai 201411 j Autor Habe etwas probiert, aber vermute das es nicht richtig ist: Das sieht dann grafisch so aus: Bearbeitet 7. Mai 201411 j von socressor
7. Mai 201411 j Mach nicht gleich alle Berechnungen in einem Ausdruck. Berechne erst einmal den horizontalen und vertikalen Abstand. $dataA[1] ist keines von beiden.
Archiv
Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.