Skip to content

Commit

Permalink
Jeddic#6 Update classes to follow the coding standards of particle mo…
Browse files Browse the repository at this point in the history
…nkey
  • Loading branch information
richardTingle committed Dec 18, 2021
1 parent 5a65fba commit e9b8408
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

public class CurveBuilderPiece{

boolean used = false;
boolean used = false;

protected void checkReuse(){
if (used){
throw new IllegalStateException("Curve builders must not be reused (As they actually build a single curve as they go along)");
}
used = true;
protected void checkReuse(){
if (used){
throw new IllegalStateException("Curve builders must not be reused (As they actually build a single curve as they go along)");
}
used = true;
}
}
148 changes: 74 additions & 74 deletions src/test/java/com/epaga/particles/valuetypes/CurveTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,79 +7,79 @@

public class CurveTest{

@Test
public void builder_straightLine(){
Curve curve = Curve.builder()
.anchorPoint(0,0)
.anchorPoint(1,10)
.build();

assertEquals(0, curve.getValue(0f), 0.001);
assertEquals(4, curve.getValue(0.4f), 0.001);
assertEquals(10, curve.getValue(1f), 0.001);
}

/**
* Tests that 2 straight lines joined together functions correctly
*/
@Test
public void builder_doubleStraightLine(){
Curve curve = Curve.builder()
.anchorPoint(0,0)
.anchorPoint(0.4f,10)
.anchorPoint(1f, 10)
.build();

assertEquals(0, curve.getValue(0f), 0.001);
assertEquals(5, curve.getValue(0.2f), 0.001);
assertEquals(10, curve.getValue(0.8f), 0.001);
}

/**
* Tests that a Bézier-like curve functions correctly
*
* (Its not actually a true Bézier curve becuse a Bézier curve can "go backwards" and follows a
* slightly different path
*/
@Test
public void builder_curve(){

Curve curve = Curve.builder()
.anchorPoint(0,0)
.controlPoint1(0.2f, 1)
.controlPoint2(0.8f, 0)
.anchorPoint(1,1)
.build();

//expected values obtained using https://www.desmos.com/calculator/ebdtbxgbq0

assertEquals(0, curve.getValue(0f), 0.001);

//value obtained as 0.1 along using the following
// along line 1 = 0.9 * 0 + 0.1 * 1 = 0.1
// along line 2 = 0.9 * 1 + 0.1 * 0 = 0.9
// along line 3 = 0.9 * 0 + 0.1 * 1 = 0.1

//obtain 2 new lines between along line 1 -> along line 2 and along line 2 -> along line 3. Get 0.1 along each one
//along second order 1 = 0.9 * 0.1 + 0.1 * 0.9 = 0.18
//along second order 2 = 0.9 * 0.9 + 0.1 * 0.1 = 0.82

//final result is 0.1 along the line between the second order points
// 0.9 * 0.18 + 0.1 * 0.82

assertEquals(0.244, curve.getValue(0.1f), 0.001);

assertEquals(0.5, curve.getValue(0.5f), 0.001);
assertEquals(1, curve.getValue(1), 0.001);
}

@Test(expected = IllegalStateException.class)
public void builder_reuseLeadsToException(){
CurveBuilderAtAnchor builder = Curve.builder()
.anchorPoint(0,0);

Curve legalUse = builder.build();
Curve illegalReuse = builder.build();
}
@Test
public void builder_straightLine(){
Curve curve = Curve.builder()
.anchorPoint(0,0)
.anchorPoint(1,10)
.build();

assertEquals(0, curve.getValue(0f), 0.001);
assertEquals(4, curve.getValue(0.4f), 0.001);
assertEquals(10, curve.getValue(1f), 0.001);
}

/**
* Tests that 2 straight lines joined together functions correctly
*/
@Test
public void builder_doubleStraightLine(){
Curve curve = Curve.builder()
.anchorPoint(0,0)
.anchorPoint(0.4f,10)
.anchorPoint(1f, 10)
.build();

assertEquals(0, curve.getValue(0f), 0.001);
assertEquals(5, curve.getValue(0.2f), 0.001);
assertEquals(10, curve.getValue(0.8f), 0.001);
}

/**
* Tests that a Bézier-like curve functions correctly
*
* (Its not actually a true Bézier curve becuse a Bézier curve can "go backwards" and follows a
* slightly different path
*/
@Test
public void builder_curve(){

Curve curve = Curve.builder()
.anchorPoint(0,0)
.controlPoint1(0.2f, 1)
.controlPoint2(0.8f, 0)
.anchorPoint(1,1)
.build();

//expected values obtained using https://www.desmos.com/calculator/ebdtbxgbq0

assertEquals(0, curve.getValue(0f), 0.001);

//value obtained as 0.1 along using the following
// along line 1 = 0.9 * 0 + 0.1 * 1 = 0.1
// along line 2 = 0.9 * 1 + 0.1 * 0 = 0.9
// along line 3 = 0.9 * 0 + 0.1 * 1 = 0.1

//obtain 2 new lines between along line 1 -> along line 2 and along line 2 -> along line 3. Get 0.1 along each one
//along second order 1 = 0.9 * 0.1 + 0.1 * 0.9 = 0.18
//along second order 2 = 0.9 * 0.9 + 0.1 * 0.1 = 0.82

//final result is 0.1 along the line between the second order points
// 0.9 * 0.18 + 0.1 * 0.82

assertEquals(0.244, curve.getValue(0.1f), 0.001);

assertEquals(0.5, curve.getValue(0.5f), 0.001);
assertEquals(1, curve.getValue(1), 0.001);
}

@Test(expected = IllegalStateException.class)
public void builder_reuseLeadsToException(){
CurveBuilderAtAnchor builder = Curve.builder()
.anchorPoint(0,0);

Curve legalUse = builder.build();
Curve illegalReuse = builder.build();
}

}

0 comments on commit e9b8408

Please sign in to comment.