changeset 1969:7f4a5146c84c feature/grids/geometry_functions

Add tests and better constructor for Circle
author Jonatan Werpers <jonatan@werpers.com>
date Fri, 14 Feb 2025 08:23:00 +0100
parents 35cb503985b6
children deeb61325320
files src/Grids/geometry.jl test/Grids/geometry_test.jl
diffstat 2 files changed, 34 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/src/Grids/geometry.jl	Wed Feb 12 15:45:37 2025 +0100
+++ b/src/Grids/geometry.jl	Fri Feb 14 08:23:00 2025 +0100
@@ -64,9 +64,16 @@
     return [LineSegment(ps[i], ps[mod1(i+1,n)]) for i ∈ eachindex(ps)]
 end
 
-struct Circle{T,PT}
+struct Circle{PT,T}
     c::PT
     r::T
+
+    Circle{PT,T}(c,r) where {PT,T} = new{PT,T}(c,r)
+end
+
+function Circle(c,r)
+    c = SVector{2}(c)
+    return Circle{typeof(c), typeof(r)}(c,r)
 end
 
 function (C::Circle)(θ)
--- a/test/Grids/geometry_test.jl	Wed Feb 12 15:45:37 2025 +0100
+++ b/test/Grids/geometry_test.jl	Fri Feb 14 08:23:00 2025 +0100
@@ -1,5 +1,5 @@
 using Diffinitive.Grids
-using Diffinitive.Grids: Line, LineSegment, linesegments, polygon_edges
+using Diffinitive.Grids: Line, LineSegment, linesegments, polygon_edges, Circle
 using StaticArrays
 
 @testset "Line" begin
@@ -85,9 +85,33 @@
 
 @testset "Circle" begin
     @testset "Constructors" begin
+        @test Circle([1,2], 1) isa Circle{SVector{2,Int},Int}
+        @test Circle([1,2], 1.) isa Circle{SVector{2,Int},Float64}
+        @test Circle([1,2.], 1.) isa Circle{SVector{2,Float64},Float64}
+        @test Circle([1,2.], 1) isa Circle{SVector{2,Float64},Int}
+        @test Circle((1,2.), 1.) isa Circle{SVector{2,Float64},Float64}
+        @test Circle((1,2), 1.) isa Circle{SVector{2,Int},Float64}
+        @test Circle((1.,2), 1) isa Circle{SVector{2,Float64},Int}
+        @test Circle((1,2), 1) isa Circle{SVector{2,Int},Int}
+        @test Circle(@SVector[1,2], 1.) isa Circle{SVector{2,Int},Float64}
+        @test Circle(@SVector[1,2.], 1.) isa Circle{SVector{2,Float64},Float64}
     end
 
-    @test_broken false
+    @testset "Evaluation" begin
+        c = Circle([0,0], 1)
+        @test c(0) ≈ [1,0]
+        @test c(π/2) ≈ [0,1]
+        @test c(π) ≈ [-1,0]
+        @test c(3π/2) ≈ [0,-1]
+        @test c(π/4) ≈ [1/√(2),1/√(2)]
+
+        c = Circle([0,0], 2)
+        @test c(0) ≈ [2,0]
+        @test c(π/2) ≈ [0,2]
+        @test c(π) ≈ [-2,0]
+        @test c(3π/2) ≈ [0,-2]
+        @test c(π/4) ≈ [√(2),√(2)]
+    end
 end
 
 @testset "TransfiniteInterpolationSurface" begin