WeightedEdge edge = triangle.getEdgeList().get(0);
LinearFunctionParameters linearParameters = SteinersAlgorithm.getLinearFunctionParameters(edge.getStart(), edge.getEnd());
// find node which does not belong to edge - another node will have one
Node oppositeSideNode;
if (edge.containsNode(triangle.getEdgeList().get(1).getStart())) {
oppositeSideNode = triangle.getEdgeList().get(1).getEnd();
} else {
oppositeSideNode = triangle.getEdgeList().get(1).getStart();
}
int oppositeSide;
// special case when we got a vertical line
if (linearParameters.getDx().compareTo(BigDecimal.ZERO) == 0) {
oppositeSide = oppositeSideNode.getPointX().compareTo(edge.getStart().getPointX());
} else {
// is (k * x + b - y) > 0
oppositeSide = linearParameters.getK()
.multiply(oppositeSideNode.getPointX())
.add(linearParameters.getB())
.subtract(oppositeSideNode.getPointY())
.compareTo(BigDecimal.ZERO);
}
BigDecimal Cos60 = BigDecimal.valueOf(Math.cos(Math.toRadians(60))).round(new MathContext(ROUNDING_PRECISION, ROUNDING_MODE));
BigDecimal Sin60 = BigDecimal.valueOf(Math.sin(Math.toRadians(60))).round(new MathContext(ROUNDING_PRECISION, ROUNDING_MODE));
// try to turn point clockwise to find coordinates of the third triangle's point
BigDecimal turnedX = Cos60.multiply(linearParameters.getDx()).subtract(Sin60.multiply(linearParameters.getDy())).add(edge.getStart().getPointX());
BigDecimal turnedY = Sin60.multiply(linearParameters.getDx()).add(Cos60.multiply(linearParameters.getDy())).add(edge.getStart().getPointY());
// check if points are in different sides
// special case when line is vertical
int thirdNodeSide;
if (linearParameters.getDx().compareTo(BigDecimal.ZERO) == 0) {
thirdNodeSide = turnedX.compareTo(edge.getStart().getPointX());
} else {
thirdNodeSide = linearParameters.getK()
.multiply(turnedX)
.add(linearParameters.getB())
.subtract(turnedY)
.compareTo(BigDecimal.ZERO);
}
// if points are in the same side, need to turn to opposite side
if (oppositeSide == thirdNodeSide) {
turnedX = Cos60.multiply(linearParameters.getDx()).add(Sin60.multiply(linearParameters.getDy())).add(edge.getStart().getPointX());
turnedY = Sin60.negate().multiply(linearParameters.getDx()).add(Cos60.multiply(linearParameters.getDy())).add(edge.getStart().getPointY());
}
Node newNode = new Node(turnedX, turnedY);
Graph<WeightedEdge> equilateralTriangle = new Graph<WeightedEdge>();
equilateralTriangle.addEdgeWithNodes(edge);
equilateralTriangle.addEdgeWithNodes(new WeightedEdge(new Edge(edge.getStart(), newNode), edge.getWeight()));
equilateralTriangle.addEdgeWithNodes(new WeightedEdge(new Edge(edge.getEnd(), newNode), edge.getWeight()));