/
shvidkuli
/
kkrjava
Обзор
Документация
Войти
/
shvidkuli
/
kkrjava
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/test/java/com/montecarlo/tests/ArcTest.java
174 строки
6 KB
kroc400
first_commit
30 апр 2026, 22:44
30 апр 2026, 22:44
2347fdb
Код
Авторство
О чём код?
package com.montecarlo.tests; import com.montecarlo.geometry.Arc; import com.montecarlo.geometry.BoundingBox; import com.montecarlo.geometry.Point; import org.junit.Test; import static org.junit.Assert.*; /** * Unit tests for {@link Arc} (semicircle / half-disk). * * <p>Default semicircle used in figure "debf": * <pre> * Diameter endpoints: F = (2, 0), E = (4, 2) * Centre = (3, 1), radius = √2 * Inward normal points towards D = (0, 4) * → normal direction ≈ (−1/√2, 1/√2) * → A point (x,y) is inside iff (x−3)²+(y−1)² ≤ 2 AND y−x+2 ≥ 0 * Area = π·(√2)²/2 = π * </pre> * </p> */ public class ArcTest { private static final double SQRT2 = Math.sqrt(2.0); // Default semicircle: centre=(3,1), r=√2, reference point D=(0,4) private static final Arc SEMI = new Arc( new Point(3, 1), SQRT2, new Point(0, 4) // D — reference on the filled side ); // ----------------------------------------------------------------------- // Area // ----------------------------------------------------------------------- @Test public void testArea() { assertEquals("Area of default semicircle must be π", Math.PI, SEMI.analyticalArea(), 1e-12); } @Test public void testAreaUnitSemicircle() { Arc unit = new Arc(new Point(0, 0), 1.0, new Point(0, 1)); assertEquals(Math.PI / 2, unit.analyticalArea(), 1e-12); } // ----------------------------------------------------------------------- // contains — diameter endpoints (on boundary) // ----------------------------------------------------------------------- @Test public void testContainsDiameterEndpointF() { // F = (2, 0): on the arc boundary assertTrue(SEMI.contains(new Point(2, 0))); } @Test public void testContainsDiameterEndpointE() { // E = (4, 2): on the arc boundary assertTrue(SEMI.contains(new Point(4, 2))); } // ----------------------------------------------------------------------- // contains — centre (on diameter chord → boundary) // ----------------------------------------------------------------------- @Test public void testContainsCentre() { // Centre (3,1): on the diameter chord, distance = 0 assertTrue(SEMI.contains(new Point(3, 1))); } // ----------------------------------------------------------------------- // contains — points strictly inside // ----------------------------------------------------------------------- @Test public void testContainsInteriorPoint() { // (2, 2): dist from (3,1) = √2, on D-side: 2−2+2=2 ≥ 0 → inside (on arc) assertTrue(SEMI.contains(new Point(2, 2))); } @Test public void testContainsInteriorCenter() { // (2.5, 1.5): dist² = 0.25+0.25=0.5 < 2; D-side: 1.5−2.5+2=1 ≥ 0 → inside assertTrue(SEMI.contains(new Point(2.5, 1.5))); } @Test public void testContainsTopCardinal() { // Top of circle: (3, 1+√2) — should be on D-side (y−x+2 = √2 ≥ 0) assertTrue(SEMI.contains(new Point(3, 1 + SQRT2))); } @Test public void testContainsLeftCardinal() { // Left of circle: (3−√2, 1) — should be on D-side assertTrue(SEMI.contains(new Point(3 - SQRT2, 1))); } // ----------------------------------------------------------------------- // contains — points outside // ----------------------------------------------------------------------- @Test public void testNotContainsB() { // B = (4, 0): on arc of circle but on the WRONG (B) side: 0−4+2=−2 < 0 assertFalse(SEMI.contains(new Point(4, 0))); } @Test public void testNotContainsRightCardinal() { // Right of circle (3+√2, 1): B-side assertFalse(SEMI.contains(new Point(3 + SQRT2, 1))); } @Test public void testNotContainsBottomCardinal() { // Bottom of circle (3, 1−√2): B-side assertFalse(SEMI.contains(new Point(3, 1 - SQRT2))); } @Test public void testNotContainsFarAwayPoint() { assertFalse(SEMI.contains(new Point(10, 10))); } @Test public void testNotContainsOrigin() { // (0,0): distance from centre = √(9+1) = √10 >> √2 assertFalse(SEMI.contains(new Point(0, 0))); } // ----------------------------------------------------------------------- // Bounding box // ----------------------------------------------------------------------- @Test public void testBoundingBoxMinX() { // Leftmost: 3 − √2 ≈ 1.586 assertEquals(3 - SQRT2, SEMI.getBoundingBox().getMinX(), 1e-10); } @Test public void testBoundingBoxMinY() { // Bottommost D-side point: min(ep1.y, ep2.y) = min(2, 0) = 0 assertEquals(0.0, SEMI.getBoundingBox().getMinY(), 1e-10); } @Test public void testBoundingBoxMaxX() { // Rightmost D-side point: max(ep1.x, ep2.x) = max(4, 2) = 4 assertEquals(4.0, SEMI.getBoundingBox().getMaxX(), 1e-10); } @Test public void testBoundingBoxMaxY() { // Topmost: 1 + √2 ≈ 2.414 assertEquals(1 + SQRT2, SEMI.getBoundingBox().getMaxY(), 1e-10); } // ----------------------------------------------------------------------- // Constructor guard // ----------------------------------------------------------------------- @Test(expected = IllegalArgumentException.class) public void testConstructorRefPointAtCentre() { // refPoint == centre should throw new Arc(new Point(0, 0), 1.0, new Point(0, 0)); } }