![]() |
SnapToGrid
Ich habe ein Objekt, welches ich auf einem anderen verschieben kann. Beim Verschieben wird ein entsprechendes Ereignis ausgelöst. Dort bekomme ich die aktuellen Koordinaten des Objektes. Jetzt habe ich ein Raster und ich würde gerne das Objekt an dem Raster ausrichten:
Delphi-Quellcode:
Die 50 muss natürlich abhängig sein von der Rasterweite. Wie kann ich ermitteln welcher gedachten Rasterlinie das Objekt am nächsten ist?
procedure TForm1.ImageEnVectLayerNotify(Sender: TObject; layer: Integer; event: TIELayerEvent);
var x : Integer; begin case event of ielMoved: begin x := ImageEnVect.Layers[ImageEnVect.LayersCurrent].PosX; ImageEnVect.Layers[ImageEnVect.LayersCurrent].PosX := 50; end; end; end; Gezeichnet wird das Raster so:
Delphi-Quellcode:
procedure TDWFotoBook.PaintGrid(Show: Boolean = True);
var i : Integer; j: Integer; begin // deselect all layers, otherwise we would also draw on the selected layers FImageEnVect.LayersCurrent := 0; // set pen mode if Show then begin FImageEnVect.Bitmap.Canvas.Pen.Mode := pmBlack end else FImageEnVect.Bitmap.Canvas.Pen.Mode := pmNotXor; with FImageEnVect do begin // draw dots for i := 0 to Width div FGridWidth do begin for j := 0 to Height div FGridWidth do begin Bitmap.Canvas.Pixels[i * FGridWidth, j * FGridWidth] := clBlack; end; end; Update; end; Self.FShowGrid := True; end; |
Re: SnapToGrid
Zitat:
Wenn du einen Punkt (x,y) auf deinem Raster hast, könntest du so die nächste Linie errechnen: a:= x div 50; // (den Punkt liegt also rechts von der a.ten Linie) b:= x-a*50; if b<25 then (a ist deine Linie) else ((a+1) ist deine Linie) |
Re: SnapToGrid
Ja, das kommt dem schon recht nahe. Nur wird nicht zur nächsten Rasterlinie ge"snappt", sondern zu einer die wesentlich weiter links davon liegt. Mein Code:
Delphi-Quellcode:
procedure TForm1.ImageEnVectLayerNotify(Sender: TObject; layer: Integer; event: TIELayerEvent);
var x : Integer; a: Integer; b: Integer; begin case event of ielMoved: begin x := ImageEnVect.Layers[ImageEnVect.LayersCurrent].PosX; a := x div FotoBook.GridWidth; b := x - a * FotoBook.GridWidth; Caption := IntToStr(a) + '/' + IntToStr(b); if b < (FotoBook.GridWidth div 2) then ImageEnVect.Layers[ImageEnVect.LayersCurrent].PosX := a else ImageEnVect.Layers[ImageEnVect.LayersCurrent].PosX := a + FotoBook.GridWidth; end; end; end; |
Re: SnapToGrid
So geht es:
Delphi-Quellcode:
Danke für den Denkanstoss.
procedure TDWFotoBook.SnapToGrid;
var x : Integer; a : Integer; b : Integer; begin x := FImageEnVect.Layers[ImageEnVect.LayersCurrent].PosX; a := x div Self.GridWidth; b := x - a * Self.GridWidth; if b < (Self.GridWidth div 2) then ImageEnVect.Layers[ImageEnVect.LayersCurrent].PosX := a * Self.GridWidth else ImageEnVect.Layers[ImageEnVect.LayersCurrent].PosX := (a + 1) * Self.GridWidth; x := ImageEnVect.Layers[ImageEnVect.LayersCurrent].PosY; a := x div Self.GridWidth; b := x - a * Self.GridWidth; if b < (Self.GridWidth div 2) then ImageEnVect.Layers[ImageEnVect.LayersCurrent].PosY := a * Self.GridWidth else ImageEnVect.Layers[ImageEnVect.LayersCurrent].PosY := (a + 1) * Self.GridWidth; end; |
Re: SnapToGrid
Es würde IMHO effizienter gehen, indem du einfach die Position durch die Gitterbreite teilst, auf den nächsten Ganzzahlwert rundest und dann einfach wieder mit der Gitterbreite multiplizierst.
|
Re: SnapToGrid
Dann würd es aber nicht immer zur nächsten Linie springen, sondern zu nächst-linken Linie!
Edit: Vergiss was ich gesgat habe, das wäre bei DIV so... :wall: |
Re: SnapToGrid
Zitat:
Delphi-Quellcode:
Das ist doch 1:1 mein Code... (Du hast im ersten Code nur die Nummer einer Linie mit seiner Koordinate verwechselt)
a := x div Self.GridWidth;
b := x - a * Self.GridWidth; if b < (Self.GridWidth div 2) then ImageEnVect.Layers[ImageEnVect.LayersCurrent].PosX := a * Self.GridWidth else ImageEnVect.Layers[ImageEnVect.LayersCurrent].PosX := (a + 1) * Self.GridWidth; |
Re: SnapToGrid
Zitat:
|
Alle Zeitangaben in WEZ +1. Es ist jetzt 08:30 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz