Skip to content

Interactive 2D wave optics simulator. Turn any image into a refractive scene, add sources, watch waves refract, diffract and interfere. Probes, intensity maps, video export.

C#

A dragged lens radius is rounded to a hundredth of a pixel

Alexander Graef36e6e5457407
diff --git a/OpticalParts.md b/OpticalParts.md
index aa3e014..4cabeac 100644
--- a/OpticalParts.md
+++ b/OpticalParts.md
@@ -90,7 +90,10 @@ stretched lens a slab). A handle on each surface's vertex sets its radius
(`SetVertex`, Jeroen's proposal): dragged along the axis with the rim and the other surface held, the sag at the rim
gives the radius by the sagitta, outward bulging, through the rim plane flat, then concave, a hemisphere at most; the
centre thickness follows and the position moves to the new centre. A drag holds the rim and the other vertex as it
-found them (`MarkerThumb.DragAnchors`), so the position's rounding never creeps into them.
+found them (`MarkerThumb.DragAnchors`), so the position's rounding never creeps into them. A radius a grip or a vertex
+handle sets is rounded to a hundredth of a pixel (`RadiusOfSag`; 2026-10-28, Jeroen's report: sixteen decimals in the
+box and the file), R1 towards +∞ and R2 towards −∞, so a convex surface gets a hair flatter and a concave one a hair
+deeper, never the other way, and the rounding never brings on the refit of a lens whose surfaces meet at the rim.
**Shapes** (`ShapePart`: `Mirror`, `RefractiveIndex` 1.50, `Coating`, `Absorption`, `AbsorptionRamp` 40, over an
outline each kind generates). One class per shape, not a shape mode inside one class (the user's point, 2026-10-18: a
diff --git a/WaveDudeNext/Model/OpticalPart.cs b/WaveDudeNext/Model/OpticalPart.cs
index be7ffd5..4bfd506 100644
--- a/WaveDudeNext/Model/OpticalPart.cs
+++ b/WaveDudeNext/Model/OpticalPart.cs
@@ -533,8 +533,8 @@ public sealed class LensPart : OpticalPart
/// the lens across, the profile along the axis held; the thickness stretches the whole profile along the axis, the sags
/// and the edge with it, so a thinner lens is the same lens squashed and never a slab; pulled out diagonally the lens
/// scales as a whole, its shape kept. The surfaces are refitted as circles through the stretched rim and vertex (a sag
- /// beyond the half diameter is a hemisphere's). Nothing stops the grip but a thickness of 1. Typed values are fitted as
- /// before.
+ /// beyond the half diameter is a hemisphere's), the radii to a hundredth of a pixel (<see cref="RadiusOfSag"/>). Nothing
+ /// stops the grip but a thickness of 1. Typed values are fitted as before.
/// </summary>
/// <param name="diameter">The diameter asked for, in pixels.</param>
/// <param name="thickness">The centre thickness asked for, in pixels.</param>
@@ -557,14 +557,27 @@ public sealed class LensPart : OpticalPart
Thickness = Math.Max(1, thickness);
}
Diameter = Math.Max(2, diameter);
- R1 = RadiusOfSag(grown, sag1);
- R2 = RadiusOfSag(grown, sag2);
+ R1 = RadiusOfSag(1, grown, sag1);
+ R2 = RadiusOfSag(2, grown, sag2);
Thickness = Math.Max(1, thickness);
}
- /// <summary>The radius, signed as <paramref name="sag"/> is, whose surface has that sag at <paramref name="half"/> from the axis: the sagitta (h² + s²) / 2s; 0 for no sag.</summary>
- private static double RadiusOfSag(double half, double sag) =>
- sag == 0.0 ? 0.0 : Math.CopySign((half * half + sag * sag) / (2.0 * Math.Abs(sag)), sag);
+ /// <summary>
+ /// The radius, signed as <paramref name="sag"/> is, whose surface <paramref name="surface"/> (1 or 2) has that sag at
+ /// <paramref name="half"/> from the axis: the sagitta (h² + s²) / 2s; 0 for no sag. Rounded to a hundredth of a pixel
+ /// (2026-10-28, Jeroen's report: a dragged vertex or grip left sixteen decimals in the box and the file), surface 1's
+ /// towards +∞ and surface 2's towards −∞, which makes a convex surface a hair flatter and a concave one a hair deeper, never
+ /// the other way, so the rounding never brings on the refit of <see cref="FitRadii"/> for a lens whose surfaces meet at the rim.
+ /// </summary>
+ private static double RadiusOfSag(int surface, double half, double sag)
+ {
+ if (sag == 0.0)
+ {
+ return 0.0;
+ }
+ double hundredths = Math.CopySign((half * half + sag * sag) / (2.0 * Math.Abs(sag)), sag) * 100.0;
+ return (surface == 1 ? Math.Ceiling(hundredths) : Math.Floor(hundredths)) / 100.0;
+ }
/// <summary>Where the vertex of surface <paramref name="surface"/> (1 the left, 2 the right) sits on the axis in the part frame: ∓<see cref="Thickness"/> / 2.</summary>
public double VertexX(int surface) => (surface == 1 ? -1 : 1) * Math.Max(1, Thickness) / 2.0;
@@ -597,7 +610,7 @@ public sealed class LensPart : OpticalPart
sag = 0.0;
}
// the sagitta: surface 1 bulging left is R1 > 0, surface 2 bulging right is R2 < 0
- double radius = RadiusOfSag(half, -side * sag);
+ double radius = RadiusOfSag(surface, half, -side * sag);
bool thicker = thickness > Thickness;
if (thicker)
{
diff --git a/_tools/library_check/Program.cs b/_tools/library_check/Program.cs
index 2ec9254..74c7368 100644
--- a/_tools/library_check/Program.cs
+++ b/_tools/library_check/Program.cs
@@ -2599,8 +2599,8 @@ internal static class Program
$"dragged wider alone the sags of 10 stay and the radii follow the diameter ({gripped.R1}), the thickness and the edge as they were");
gripped = new LensPart { Diameter = 100, Thickness = 20, R1 = 130, R2 = -130 };
gripped.SetExtents(100, 60);
- Check(Math.Abs(gripped.R1 - 3400.0 / 60.0) < 1e-9 && gripped.Thickness == 60 && gripped.Diameter == 100 && Math.Abs(gripped.EdgeThickness) < 1e-9,
- $"dragged thicker alone the whole profile scales: sags of 30, R {gripped.R1}, the edge still zero");
+ Check(gripped.R1 == 56.67 && gripped.R2 == -56.67 && gripped.Thickness == 60 && gripped.Diameter == 100 && gripped.EdgeThickness >= 0.0 && gripped.EdgeThickness < 0.01,
+ $"dragged thicker alone the whole profile scales: sags of 30, R 3400 / 60 to a hundredth, {gripped.R1}, the edge still zero within a hundredth ({gripped.EdgeThickness})");
gripped = new LensPart { Diameter = 100, Thickness = 20, R1 = 130, R2 = -130 };
gripped.SetExtents(200, 40);
Check(Math.Abs(gripped.R1 - 260) < 1e-9 && Math.Abs(gripped.R2 + 260) < 1e-9, $"pulled out diagonally the lens scales as a whole: the radii doubled to {gripped.R1}");
@@ -2608,6 +2608,13 @@ internal static class Program
Check(gripped.Thickness == 4 && Math.Abs(gripped.R1 - (10000 + 4) / 4.0) < 1e-9 && Math.Abs(gripped.EdgeThickness) < 1e-9, $"squashed to 4 thick it is the same lens squashed, sags of 2, R {gripped.R1}, never a slab");
gripped.SetExtents(100, 400);
Check(gripped.R1 == 50 && gripped.R2 == -50 && gripped.Thickness == 400, "stretched thicker than its surfaces can take at that diameter, they are hemispheres on a slab");
+ // a dragged radius is rounded to a hundredth of a pixel away from the lens (2026-10-28, Jeroen's report: sixteen decimals
+ // in the box): 3725 / 70 = 53.214... becomes 53.22, not 53.21, whose deeper sags would have had the surfaces refitted
+ gripped = new LensPart { Diameter = 100, Thickness = 20, R1 = 130, R2 = -130 };
+ gripped.SetExtents(100, 70);
+ gripped.Normalize();
+ Check(gripped.R1 == 53.22 && gripped.R2 == -53.22 && gripped.EdgeThickness >= 0.0 && gripped.EdgeThickness < 0.05,
+ $"a dragged radius is rounded to a hundredth of a pixel on the flatter side, 3725 / 70 giving {gripped.R1}, which Normalize keeps since the surfaces still fit (the edge a few hundredths, {gripped.EdgeThickness}: so curved a surface moves its rim by twice the rounding)");
// a surface handle: the vertex moves on the axis, the rim and the other surface stay; the position follows the centre
var shaped = new LensPart { X = 500, Y = 300, Diameter = 100, Thickness = 20, R1 = 0, R2 = 0 };
shaped.SetVertex(1, -30.0);
@@ -2616,11 +2623,16 @@ internal static class Program
shaped.SetVertex(1, 0.0);
Check(shaped.R1 == 0 && shaped.Thickness == 20 && shaped.X == 500, "pushed back to the rim plane it is flat again, the slab as before");
shaped.SetVertex(1, 5.0);
- Check(Math.Abs(shaped.R1 + 2725.0 / 30.0) < 1e-9 && shaped.Thickness == 5 && shaped.X == 508 && Math.Abs(shaped.EdgeThickness - 20) < 1e-9, $"pushed 15 px past the rim plane it is concave, R1 {shaped.R1} (the sagitta of 15), 5 thick at the centre, the edge still 20");
+ Check(shaped.R1 == -90.83 && shaped.Thickness == 5 && shaped.X == 508 && shaped.EdgeThickness >= 20.0 && shaped.EdgeThickness < 20.01, $"pushed 15 px past the rim plane it is concave, R1 {shaped.R1} (the sagitta of 15, 2725 / 30, to a hundredth towards the deeper side), 5 thick at the centre, the edge still 20 within a hundredth");
shaped.SetVertex(2, 1000.0);
Check(shaped.R2 == -50 && shaped.Thickness > 500, $"the right vertex pulled far out stops at a hemisphere, R2 {shaped.R2}");
shaped.SetVertex(2, -2000.0);
Check(shaped.Thickness == 1, "pushed through the other surface it stops a pixel short of it");
+ // the rim a drag captured lies where the rotation and the position's rounding left it, a fraction of a pixel off, and the
+ // radius through it has sixteen decimals before the rounding (29.7 of sag at 50 gives 56.9375...)
+ shaped = new LensPart { X = 500, Y = 300, Diameter = 100, Thickness = 20, R1 = 0, R2 = 0 };
+ shaped.SetVertex(1, -30.0, -0.3, 10.0);
+ Check(shaped.R1 == 56.94 && shaped.Thickness == 40, $"a vertex dragged against a rim a fraction of a pixel off gets a radius of two decimals, {shaped.R1}, rounded up");
lens.Diameter = 100;
lens.Thickness = 40;
lens.R1 = 300;