PDA

Archiv verlassen und diese Seite im Standarddesign anzeigen : Koordinaten-problem


The Serious Sam
27.02.2008, 22:42:56
Hi

Ich habe ein Problem bei dem ich nicht weiter komme^^

Ich habe eine datenbank, in der felder einer karte gespeichert werden



x | y | terrain

z.B:

-3 | 24 | gras.png



Ich rufe nun immer ein paar dieser Felder ab (je nachdem welche x/y werte angegeben sin)


$cx = (int)@$_GET['x'];
$cy = (int)@$_GET['y'];
$lx = $cx-5; // linksscrollwert
$rx = $cx+5; // rechtsscrollwert
$oy = $cy-5; // obenscrollwert
$uy = $cy+5; // untenscrollwert

$sql = "
SELECT
*
FROM
map
WHERE
x>=$cx-6
AND
x<=$cx+6
AND
y>=$cy-6
AND
y<=$cy+6
";

// Nur die nötigen werte raussuchen

$qry = new Query($sql);

while($row = $qry->fetch()) {

$map[$row['y']][$row['x']] = $row['terrain']; // in einem geeignetem(?) Array speichern

}



nun wir das ganze super schön :D ausgegeben mit vielen tollen for-schleifen

(Bin hier für bessere lösungsansätze sehr dankbar)


echo "<table cellpadding=\"0\" cellspacing=\"0\" id=\"map\">\n";

echo " <tr>\n";
for($i=1;$i<=8;$i++) echo " <td></td>\n";
echo " <td><a href='map.test.php?x=$cx&y=$oy'><img src=\"map_n.png\" alt=\"map_n\"></a></td>\n";
for($i=1;$i<=6;$i++) echo " <td></td>\n";
echo " </tr>\n";
echo " <tr>\n";
echo " <td></td>\n";
echo " </tr>\n";
for($y=$cy-6;$y<=$cy+6;$y++) { // y- Reihe
echo " <tr>\n";
for($x=$cx-8;$x<=$cx+7;$x++) { // x-Reihe
if($x==($cx-8)) {

if($y==$cy) { // Pfeil nach Westen
echo " <td><a href='map.test.php?x=".($cx-5)."&y=$cy'><img src=\"map_w.png\" alt=\"map_w\"></a></td>\n";
} else {
echo " <td></td>\n";
}

} elseif($x==($cx-7)) { // y-Koordinate

echo " <td>$y</td>\n";

} elseif($x==($cx+7)) {

if($y==$cy) {// Pfeil nach Osten

echo " <td><a href='map.test.php?x=$rx&y=$cy'><img src=\"map_e.png\" alt=\"map_e\"></a></td>\n";

} else { // Leerzeile

echo " <td></td>\n";

}
} else { // Keine koordinaten oder Pfeile -> Terrain ausgeben

if(!isset($map[$y][$x])) $map[$y][$x] = 'gras.png';

if(preg_match('/v[1-4][a-z]+.png/', $map[$y][$x]) || preg_match('/v[1-4].png/', $map[$y][$x])) { // bestimmte felder raussuchen und mit linkzum zentrieren versehen
echo " <td><a href='map.test.php?x=$x&y=$y'><img src=\"".$map[$y][$x]."\" alt=\"".$map[$y][$x]."\"></a></td>\n";
} else {
echo " <td><img src=\"".$map[$y][$x]."\" alt=\"".$map[$y][$x]."\"></td>\n"; // ansonsten einfach terrain ausgeben (Wald, See,...)
}
}
}
echo " </tr>\n";
}

echo " <tr>\n";
echo " <td></td>\n";
echo " <td></td>\n";
for($xk=$cx-6;$xk<=$cx+6;$xk++){ // x-koordinaten

echo " <td>$xk</td>\n";

}
echo " </tr>\n";
echo " <tr>\n";
for($i=1;$i<=8;$i++) echo " <td></td>\n";
echo " <td><a href='map.test.php?x=$cx&y=$uy'><img src=\"map_s.png\" alt=\"map_s\"></a></td>\n"; // Pfeil nach süden
for($i=1;$i<=3;$i++) echo " <td></td>\n";
echo " </tr>\n";
echo " <tr>\n";
echo " <td></td>\n";
echo " </tr>\n";

echo "</table>\n";


Da die Koordinaten und Pfeilzeilen äußerst verwirrend sind hiernocheinmal das grobgerüst:


echo "<table cellpadding=\"0\" cellspacing=\"0\" id=\"map\">\n";

for($y=$cy-6;$y<=$cy+6;$y++) { // y- Reihe
echo " <tr>\n";
for($x=$cx-6;$x<=$cx+6;$x++) { // x-Reihe
if(!isset($map[$y][$x])) $map[$y][$x] = 'gras.png';

if(preg_match('/v[1-4][a-z]+.png/', $map[$y][$x]) || preg_match('/v[1-4].png/', $map[$y][$x])) { // bestimmte felder raussuchen und mit linkzum zentrieren versehen
echo " <td><a href='map.test.php?x=$x&y=$y'><img src=\"".$map[$y][$x]."\" alt=\"".$map[$y][$x]."\"></a></td>\n";
} else {
echo " <td><img src=\"".$map[$y][$x]."\" alt=\"".$map[$y][$x]."\"></td>\n"; // ansonsten einfach terrain ausgeben (Wald, See,...)
}
}
}
echo " </tr>\n";
}


echo "</table>\n";


es wird also ein kartenausschnitt angezeigt 6felder nach norden osten süden westen.

unten wird nocheinmal eine große übersichstfarbe eingezeichnet:


echo "<div style=\"position:absolute;\">\n";
echo "<form action=\"map.test.php\">\n";
echo "<input type=\"hidden\" name=\"action\">\n";
echo "<input type=\"image\" src=\"map.color.php?x=$cx&y=$cy\">\n";
echo "</form>\n";
echo "</div>";


die map.color.php:


<?

config();

require_once('query.class.php');

$cx = (int)@$_GET['x'];
$cy = (int)@$_GET['y'];
$zx = $cx-25;
$zy = $cy-25;

$sql = "
SELECT
*
FROM
map
WHERE
x>=$cx-25
AND
x<=$cx+25
AND
y>=$cy-25
AND
y<=$cy+25
";

$qry = new Query($sql);

while($row = $qry->fetch()) {

$map[$row['y']][$row['x']] = $row['terrain'];

}

header("Content-type: image/png");
$im = ImageCreate(251, 251);
$back = ImageColorAllocate($im, 50, 100, 30);

$gras = ImageColorAllocate($im, 70, 120, 40);
$wald = ImageColorAllocate($im, 55, 105, 33);

$slf = ImageColorAllocate($im, 240, 200, 0);
$emy = ImageColorAllocate($im, 255, 0, 0);
$gry = ImageColorAllocate($im, 150, 150, 150);
$gld = ImageColorAllocate($im, 0, 0, 255);
$bnd = ImageColorAllocate($im, 0, 160, 244);
$nap = ImageColorAllocate($im, 128, 0, 128);

$black = ImageColorAllocate($im, 0, 0, 0);
for($y=1;$y<=50;$y++) {
$zx=$cx-25;
for($x=1;$x<=50;$x++) {
$x1 = $x+4*($x-1);
$y1 = $y+4*($y-1);
$x2 = $x1+3;
$y2 = $y1+3;
if(!isset($map[$zy][$zx])) $map[$zy][$zx] = 'gras.png';

if(preg_match('/v[1-4]bnd.png/', $map[$zy][$zx])) {
$color = $bnd;
} elseif(preg_match('/v[1-4]g.png/', $map[$zy][$zx])) {
$color = $gld;
} elseif(preg_match('/v[1-4]s.png/', $map[$zy][$zx])) {
$color = $slf;
} elseif(preg_match('/v[1-4]nap.png/', $map[$zy][$zx])) {
$color = $nap;
} elseif(preg_match('/v[1-4].png/', $map[$zy][$zx])) {
$color = $emy;
} elseif(preg_match('/wald.png/', $map[$zy][$zx])) {
$color = $wald;
} else {
$color = $gras;
}

ImageFilledRectangle($im, $x1, $y1, $x2, $y2, $color);
$zx++;
}
$zy++;
}

imagepng($im);

?>


und hier tritt mein problem ein:

Wie man sieht wird die große übersich als submit innerhalb eines Formulars eingesetzt
d.h. das formular wird mit den koordinaten abgeschickt, auf die der benutzer geklickt hat.

nun sollen die karten auf diesen punkt zentriert werden

Problem:
Das formular hab eine ganz andere Pixelauslösung als meine karte (Maße: 251x251)
Die koordinaten sind auch negativ (s.o. Bsp), die des submits nur positiv (halt von 0-251)

wie schaffe ich es nun die neuen koordinaten so umzurechnen, das sie wieder in mein schema passen?

ich habe ein bild angehängt wie die karte bis jetzt aussieht.

mfg

defabricator
28.02.2008, 10:15:11
Warum schickst Du denn eine große Grafik und nicht 13x13 Kacheln?

The Serious Sam
28.02.2008, 18:27:13
Die große map ist in kacheln^^ 13*13 aber die kleine unten

die obere soll dort zentriert werden, wo ich unten hingeklickt hab

defabricator
29.02.2008, 09:53:42
Dann musst Du einfach durch die "Vergrößerung" teilen und dann den Nullpunkt verschieben.
Wenn Du pro Feld ein Pixel nehmen würdest, hättest Du bei 51*51 Feldern auch 51*51 Pixel und x würde zwischen 0 und 51 liegen. Bei zwei Pixeln 0-102, bei drei Pixeln 0-153 usw.
Also musst Du den x Wert im letzten Beispiel (3 Pixel pro Feld) durch 3 teilen. Analog dazu y.
Welchen Wert bekommst Du dann für die Mitte (also cx=0)? 25. Also musst Du von x nach dem Teilen noch 25 abziehen.