package hu.kazocsaba.math.geometry.fitting;

import hu.kazocsaba.math.matrix.Vector2;
import java.util.List;
import hu.kazocsaba.math.geometry.Line2;
import hu.kazocsaba.math.matrix.immutable.ImmutableMatrixFactory;
import java.util.ArrayList;
import java.util.Random;
import org.junit.Test;
import static org.junit.Assert.*;

/**
 *
 * @author Kazó Csaba
 */
public class LineFitterTest {
	private static final double EPS=1e-8;

	@Test
	public void testFit() {
		Line2 refLine=Line2.createFromDir(ImmutableMatrixFactory.createVector(3, 4), ImmutableMatrixFactory.createVector(-5, 2));
		
		Random rnd=new Random(726);
		
		List<Vector2> points=new ArrayList<Vector2>(50);
		for (int i=0; i<50; i++) points.add(refLine.getPointAt(rnd.nextDouble()));
		
		Line2 fitLine=LineFitter.fit2(points);
		
		assertEquals(1, Math.abs(refLine.getUnitDir().dot(fitLine.getUnitDir())), EPS);
		assertEquals(0, refLine.distance(fitLine.getPoint()), EPS);
	}
}

@@ -38,16 +38,75 @@
 tting;%0A%0A
+import hu.kazocsaba.math.geometry.DegenerateCaseException;%0A
 import h
@@ -138,16 +138,16 @@
 ector2;%0A
-
 import j
@@ -202,16 +202,63 @@
 .Line2;%0A
+import hu.kazocsaba.math.matrix.MatrixFactory;%0A
 import h
@@ -311,24 +311,24 @@
 rixFactory;%0A
-
 import java.
@@ -339,24 +339,54 @@
 .ArrayList;%0A
+import java.util.Collections;%0A
 import java.
@@ -1073,10 +1073,173 @@
 PS);%0A%09%7D%0A
+%09%0A%09@Test(expected=DegenerateCaseException.class)%0A%09public void testDegenerate() %7B%0A%09%09LineFitter.fit2(Collections.nCopies(10, MatrixFactory.createVector(2, -4)));%0A%09%7D%0A
 %7D%0A

Add test for degenerate line fitter task.