Delphi-PRAXiS
Seite 2 von 2     12   

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Programmieren allgemein (https://www.delphipraxis.net/40-programmieren-allgemein/)
-   -   Ellipsenalgo aus Formelsammlung kreieren (https://www.delphipraxis.net/217478-ellipsenalgo-aus-formelsammlung-kreieren.html)

Rollo62 9. Jul 2025 08:39

AW: Ellipsenalgo aus Formelsammlung kreieren
 
Zitat:

Zitat von delphifan2004 (Beitrag 1550045)
Danke an Euch alle! So wird meine Ellipse gezeichnet. Mit 4096 Schritten passiert das lückenlos.

Genau, das ist generell die Problematik bei diesen parametriesierten Methoden.
Man will die Schrittlänge, oder "Punktdichte" ja nicht jedes Mal von Hand optimieren und vorgeben.
Bei kleinen Ellipsen sind 4096 vielleicht totaler Overhead, bei großen Ellipsen kommt es vielleicht gerade mal so hin.

Die Frage wäre deshalb, wie berechne ich die Schritte immer optimal, z.B. anhand einer gewünschten "Punktdichte" für alle Zeichenelemente auf dem Canvas ?

Dazu fallen mir mindestens 3 Alternativen ein, die aber alle nicht ideal sind, weil sie sqrt und trigonometrische Funktionen nutzen können.

Grundsätzlich denke ich, gibt es:
A.) Entweder vor dem eigentlichen Zeichen berechen/abschätzen

Delphi-Quellcode:

    function CalcNumSteps(a, b, stAngleDeg, endAngleDeg: Extended; dMax: Integer): Integer;
Das kann spitze Enden aber womöglich nicht dicht genug abbilden.

oder

B.) iterativ innerhalb der Zeichenschleife anhand der Teilstücke berechnen.
Damit könnte zumindest die "Punktdichte" auch variieren, z.B. bei extrem unsymmetrischen Ellipsen.

Vielleicht gibt es dafür ja auch eine möglichst effiziente Methode, die ich nicht kenne?

dummzeuch 9. Jul 2025 09:39

AW: Ellipsenalgo aus Formelsammlung kreieren
 
Zitat:

Zitat von Renate Schaaf (Beitrag 1550040)
Zitat:

Zitat:

Zitat von Rollo62 (Beitrag 1550035)
Vielleicht ist es besser, die Laufvariable parametrisch zu Normieren und nicht als Winkel zu übernehmen.
Dann kann man das Rendering etwass besser steuern.

Delphi-Quellcode:

    x := xm + xR * Cos(t);
    y := ym + yR * Sin(t);



Wo ich das gerade lese: Es gibt in Math eine Funktion SinCos, die Cos und Sin gleichzeitig berechnet und dadurch effizienter ist.

freimatz 9. Jul 2025 09:56

AW: Ellipsenalgo aus Formelsammlung kreieren
 
Zitat:

Zitat von delphifan2004 (Beitrag 1550045)
@himitsu: Ja, Delphi kann das auch aber ich wollte ja gerade wissen wie das Delphi intern macht und dazu ist die Ellipsengleiching notwendig.

Delphi macht das intern sicher anders. Dazu müsste man in dessen Code schauen.

Kas Ob. 9. Jul 2025 10:08

AW: Ellipsenalgo aus Formelsammlung kreieren
 
the Step should be calculated from the drawing function not to be supplied with a, b, startangle and endangle, unless you want to draw points instead of continuous line, and it should be calculated from the radius, the radius here is Sqrt(a^2+b^2)

Step = 1/Sqrt(a^2+b^2)

and that it is for continuous line, if you want a dotted draw then make the passed parameter with coefficient as factor to the step so it will be

Step = StepMultiplier/Sqrt(a^2+b^2)

Rollo62 9. Jul 2025 15:53

AW: Ellipsenalgo aus Formelsammlung kreieren
 
Zitat:

Zitat von Kas Ob. (Beitrag 1550052)
Step = 1/Sqrt(a^2+b^2)

Like said, it is the sqrt that makes me nervous, but its maybe needed here.
I think it depends a lot on the shape, how to handle it.
For lines, circles, squares, a pre-calculated step will work nicely.
For ellipses and other complex paths, maybe you always need varying step-sizes during drawing.

Kas Ob. 9. Jul 2025 16:26

AW: Ellipsenalgo aus Formelsammlung kreieren
 
Zitat:

Zitat von Rollo62 (Beitrag 1550055)
Zitat:

Zitat von Kas Ob. (Beitrag 1550052)
Step = 1/Sqrt(a^2+b^2)

Like said, it is the sqrt that makes me nervous, but its maybe needed here.
I think it depends a lot on the shape, how to handle it.
For lines, circles, squares, a pre-calculated step will work nicely.
For ellipses and other complex paths, maybe you always need varying step-sizes during drawing.

A overdraw pixel will happen for the suggested above for sure, but it will not affect the drawing, the only enhancement to do is to check if x and y changed from last pixel/point then draw, due to approximation of course, as for dynamic, it will return to the same check no matter what algorithm been used, for more accurate approximation you need to calculate the derivative of the speed of pixel on the slope then divide it by the radius, but not that radius you will calculate from (0,0) the center of the ellipse, this will be more complex for no, benefit visually or not.

the only case when it corrupt drawing is when the ellipse is more like a line then an ellipse, when one of the axis is barely moving while the other taking the full speed for the next step.

Kas Ob. 9. Jul 2025 16:30

AW: Ellipsenalgo aus Formelsammlung kreieren
 
To calculate this accurately you need to take the last used step then calculate the length to move for current pixel based on the last step, draw it then adjust the step for the next one, so in theory we will be calculating the step for the next pixel but not for the current one.

after calculating the distance from the last pixel divide by the radius and use it for the next pixel.

Rollo62 9. Jul 2025 16:55

AW: Ellipsenalgo aus Formelsammlung kreieren
 
Zitat:

Zitat von Kas Ob. (Beitrag 1550057)
the only case when it corrupt drawing is when the ellipse is more like a line then an ellipse, when one of the axis is barely moving while the other taking the full speed for the next step.

Yes, thats what I meant.
An algorithm sould be univeral and not break when it comes closer to extreme settings.
https://www.geogebra.org/m/AAx6FWka

If the dot is on top side, each of its step is maybe set to be dense enough.
If the dot is in right side, anbd the ellipse not very high, then the density will increase a lot, perhaps too dense.

Either you can accept these changes in density, or you try to optimize it in each step.
The whole world is a compromize :-D

Maybe one solution would be to divide the ellipse into sections, where its equal to circle, where its more dense and where its not so dense, for switching to different optimized algorithms in each segment.

Kas Ob. 9. Jul 2025 17:31

AW: Ellipsenalgo aus Formelsammlung kreieren
 
Zitat:

Zitat von Rollo62 (Beitrag 1550061)
Maybe one solution would be to divide the ellipse into sections, where its equal to circle, where its more dense and where its not so dense, for switching to different optimized algorithms in each segment.

Think about it, no matter how much you divide it will return to check against the last pixel or calculating the new (next) pixel distance form the current:
1) in the first it is easier to simply compare (new pixel vs last pixel) then adjust.
2) in the other you must have the distance is close enough to 1 in four straight direction (up, down, left and right) and close enough to sqrt(2) in 4 diagonal direction, any less than that we are drawing on top of the current (or the last), any bigger we are skipping.

Whole problem is due the arithmetic is in float numbers while drawing and coordination in integers.

i don't think that there is quite real one liner algorithm that can be solve this.


Alle Zeitangaben in WEZ +1. Es ist jetzt 23:49 Uhr.
Seite 2 von 2     12   

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