/**
* Copyright (c) 2009-2011, chunquedong(YangJiandong)
*
* This file is part of ChunMap project
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE(Version >=3)
*
* History:
* 2010-05-05 Jed Young Creation
*/
package chunmap.model.elem;
import static org.junit.Assert.*;
import org.junit.Test;
import chunmap.model.coord.CPoint;
import chunmap.model.coord.Coordinate2D;
import chunmap.model.elem.Line;
import chunmap.util.math.MyDouble;
/**
* @author chunquedong
*
*/
public class LineTest {
/**
* {@link chunmap.model.elem.Line#getY(double)} 的测试方法。
*/
@Test
public void testGetY() {
// y=4x+1
Line l1 = new Line(4, 1);
double y1 = l1.getY(2);
assertTrue(y1 == 9);
}
/**
* {@link chunmap.model.elem.Line#getX(double)} 的测试方法。
*/
@Test
public void testGetX() {
// y=4x+1;
CPoint p1 = new Coordinate2D(1, 5);
CPoint p2 = new Coordinate2D(-1, -3);
Line l1 = new Line(p1, p2);
double x1 = l1.getX(9);
assertTrue(MyDouble.approximateEquals(x1, 2));
}
/**
* 测试水平线
*/
@Test
public void testCritical() {
// y=5;
CPoint p1 = new Coordinate2D(1, 5);
CPoint p2 = new Coordinate2D(-1, 5);
Line l1 = new Line(p1, p2);
double x1 = l1.getX(9);
String s = x1 + "";
assertTrue(s.equals("NaN"));
double y1 = l1.getY(10);
assertTrue(MyDouble.approximateEquals(y1, 5));
}
/**
* 测试垂直线
*/
@Test
public void testCritical2() {
// x=1
CPoint p1 = new Coordinate2D(1, 5);
CPoint p2 = new Coordinate2D(1, 0.5);
Line l1 = new Line(p1, p2);
double x1 = l1.getX(9);
assertTrue(MyDouble.approximateEquals(x1, 1));
double y1 = l1.getY(10);
assertTrue(Double.isNaN(y1));
}
@Test
public void testCritical3() {
// x=1
CPoint p1 = new Coordinate2D(1, 5);
Line l1 = new Line(p1, Double.NEGATIVE_INFINITY);
double x1 = l1.getX(9);
assertTrue(MyDouble.approximateEquals(x1, 1));
double y1 = l1.getY(10);
assertTrue(Double.isNaN(y1));
}
@Test
public void testCritical4() {
Line l1 = new Line(Double.POSITIVE_INFINITY, 1);
// System.out.println(l1.getK());
double x1 = l1.getX(9);
assertTrue(MyDouble.approximateEquals(x1, 1));
double y1 = l1.getY(10);
assertTrue(Double.isNaN(y1));
}
/**
* {@link chunmap.model.elem.Line#getVerticalK()} 的测试方法。
*/
@Test
public void testGetVerticalK() {
// y=4x+1;
Coordinate2D p1 = new Coordinate2D(1, 5);
Coordinate2D p2 = new Coordinate2D(-1, -3);
Line l1 = new Line(p1, p2);
double k = l1.getVerticalK();
assertTrue(MyDouble.approximateEquals(k, -0.25));
}
/**
* {@link chunmap.model.elem.Line#crossCoordinate2D(chunmap.model.elem.Line)}
* 的测试方法。
*/
@Test
public void testCrossCoordinate2D() {
// y=4x+1;
CPoint p1 = new Coordinate2D(1, 5);
CPoint p2 = new Coordinate2D(-1, -3);
Line l1 = new Line(p1, p2);
// y=x-1;
Line l2 = new Line(1, -1);
CPoint p = new Coordinate2D(-2d / 3d, (-2d / 3d) - 1);
assertTrue(p.equals(l1.crossPoint(l2)));
assertTrue(p.equals(l2.crossPoint(l1)));
}
@Test
public void testCriticalCrossCoordinate2D() {
// y=4x+1;
CPoint p1 = new Coordinate2D(1, 5);
CPoint p2 = new Coordinate2D(1, 3);
Line l1 = new Line(p1, p2);
// y=x-1;
Line l2 = new Line(1, -1);
CPoint p = new Coordinate2D(1, 0);
assertTrue(p.equals(l1.crossPoint(l2)));
assertTrue(p.equals(l2.crossPoint(l1)));
}
@Test
public void testDistance() {
// y=x+1;
Line l = new Line(1, 1);
CPoint p = new Coordinate2D(0, 0);
double d = l.distance(p);
assertTrue(MyDouble.approximateEquals(Math.sqrt(2) / 2d, d));
}
}