Skip to content

Commit

Permalink
[#523] Use GeomUtils.round instead of Math.round
Browse files Browse the repository at this point in the history
  • Loading branch information
prmr committed May 9, 2024
1 parent b4aa789 commit ec6e82e
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 35 deletions.
10 changes: 5 additions & 5 deletions src/org/jetuml/geom/Conversions.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ private Conversions() {}
public static Point toPoint(Point2D pPoint2D)
{
assert pPoint2D != null;
return new Point( (int)Math.round(pPoint2D.getX()), (int)Math.round(pPoint2D.getY()));
return new Point( GeomUtils.round(pPoint2D.getX()), GeomUtils.round(pPoint2D.getY()));
}

/**
Expand Down Expand Up @@ -74,9 +74,9 @@ public static Rectangle toRectangle(Bounds pBounds)
public static Rectangle toRectangle(Rectangle2D pRectangle2D)
{
assert pRectangle2D != null;
return new Rectangle( (int)Math.round(pRectangle2D.getMinX()),
(int)Math.round(pRectangle2D.getMinY()),
(int)Math.round(pRectangle2D.getWidth()),
(int)Math.round(pRectangle2D.getHeight()));
return new Rectangle( GeomUtils.round(pRectangle2D.getMinX()),
GeomUtils.round(pRectangle2D.getMinY()),
GeomUtils.round(pRectangle2D.getWidth()),
GeomUtils.round(pRectangle2D.getHeight()));
}
}
5 changes: 2 additions & 3 deletions src/org/jetuml/geom/Direction.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package org.jetuml.geom;

import static java.lang.Math.acos;
import static java.lang.Math.round;
import static java.lang.Math.toDegrees;

import java.util.HashMap;
Expand Down Expand Up @@ -215,7 +214,7 @@ private static int asAngle(int pWidth, int pHeight)
// Compute the angle. We use the arccos instead of the arcsin
// despite the fact that the height is the opposing side to shift
// the angle by 90 to align 0 with north.
long degrees = round(toDegrees(acos(normalizedHeight)));
int degrees = GeomUtils.round(toDegrees(acos(normalizedHeight)));

// We negate the angle for the left half-plane
if( pWidth <= 0 )
Expand All @@ -224,7 +223,7 @@ private static int asAngle(int pWidth, int pHeight)
}

// Position the angle in the [0, 359] range
return (int) (degrees + DEGREES_IN_CIRCLE) % DEGREES_IN_CIRCLE;
return (degrees + DEGREES_IN_CIRCLE) % DEGREES_IN_CIRCLE;
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion src/org/jetuml/geom/GeomUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ else if( pDirection.isBetween(topNW, bottomNW))
*/
private static int lengthOfOpposingSide(int pAngleInDegrees, int pAdjacentSide)
{
return (int) Math.round(pAdjacentSide * Math.tan(Math.toRadians(pAngleInDegrees)));
return round(pAdjacentSide * Math.tan(Math.toRadians(pAngleInDegrees)));
}

/**
Expand Down
14 changes: 6 additions & 8 deletions src/org/jetuml/gui/ViewportProjection.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
*******************************************************************************/
package org.jetuml.gui;

import org.jetuml.geom.GeomUtils;

/**
* An instance of this class can be used to make viewport projection
* computations to compare visible areas of a scrollpane viewport with an
Expand Down Expand Up @@ -86,8 +88,7 @@ public double getHeightRatio()
*/
public int getHiddenLeft()
{
double hiddenLeft = hiddenWidth() * aHValue;
return Math.round((float) hiddenLeft);
return GeomUtils.round(hiddenWidth() * aHValue);
}

/**
Expand All @@ -96,8 +97,7 @@ public int getHiddenLeft()
*/
public int getHiddenRight()
{
double hiddenRight = hiddenWidth() * (1.0-aHValue);
return Math.round((float) hiddenRight);
return GeomUtils.round(hiddenWidth() * (1.0-aHValue));
}

/**
Expand All @@ -106,8 +106,7 @@ public int getHiddenRight()
*/
public int getHiddenTop()
{
double hiddenTop = hiddenHeight() * aVValue;
return Math.round((float) hiddenTop);
return GeomUtils.round(hiddenHeight() * aVValue);
}

/**
Expand All @@ -116,8 +115,7 @@ public int getHiddenTop()
*/
public int getHiddenBottom()
{
double hiddenBottom = hiddenHeight() * (1.0 - aVValue);
return Math.round((float) hiddenBottom);
return GeomUtils.round(hiddenHeight() * (1.0 - aVValue));
}

private int hiddenHeight()
Expand Down
7 changes: 4 additions & 3 deletions src/org/jetuml/rendering/FontMetrics.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package org.jetuml.rendering;

import org.jetuml.geom.Dimension;
import org.jetuml.geom.GeomUtils;

import javafx.geometry.Bounds;
import javafx.scene.text.Font;
Expand Down Expand Up @@ -81,7 +82,7 @@ public Dimension getDimension(String pString)

aTextNode.setText(pString);
Bounds bounds = aTextNode.getLayoutBounds();
return new Dimension((int) Math.round(bounds.getWidth()), (int) Math.round(bounds.getHeight()));
return new Dimension(GeomUtils.round(bounds.getWidth()), GeomUtils.round(bounds.getHeight()));
}

/**
Expand All @@ -99,7 +100,7 @@ public int getHeight()
double twoLineHeight = aTextNode.getLayoutBounds().getHeight();
aTextNode.setText(SINGLE_LINED_TEXT);
double singleLineHeight = aTextNode.getLayoutBounds().getHeight();
return (int) Math.round(twoLineHeight - singleLineHeight);
return GeomUtils.round(twoLineHeight - singleLineHeight);
}

/**
Expand All @@ -112,6 +113,6 @@ public int getHeight()
public int getBaselineOffset()
{
aTextNode.setText(SINGLE_LINED_TEXT);
return (int) Math.round(aTextNode.getBaselineOffset());
return GeomUtils.round(aTextNode.getBaselineOffset());
}
}
9 changes: 5 additions & 4 deletions src/org/jetuml/rendering/Grid.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

package org.jetuml.rendering;

import org.jetuml.geom.GeomUtils;
import org.jetuml.geom.Point;
import org.jetuml.geom.Rectangle;

Expand Down Expand Up @@ -74,9 +75,9 @@ public static void draw(GraphicsContext pGraphics, Rectangle pBounds)
public static Rectangle snapped(Rectangle pRectangle)
{
assert pRectangle != null;
int x = (int)(Math.round(pRectangle.getX() / GRID_SIZE) * GRID_SIZE);
int x = (int)(GeomUtils.round(pRectangle.getX() / GRID_SIZE) * GRID_SIZE);
int width = (int)(Math.ceil(pRectangle.getWidth() / GRID_SIZE) * GRID_SIZE);
int y = (int)(Math.round(pRectangle.getY() / GRID_SIZE) * GRID_SIZE);
int y = (int)(GeomUtils.round(pRectangle.getY() / GRID_SIZE) * GRID_SIZE);
int height = (int)(Math.ceil(pRectangle.getHeight() / GRID_SIZE) * GRID_SIZE);
return new Rectangle(x, y, width, height);
}
Expand All @@ -91,8 +92,8 @@ public static Rectangle snapped(Rectangle pRectangle)
public static Point snapped(Point pPoint)
{
assert pPoint != null;
int x = (int)(Math.round(pPoint.x() / GRID_SIZE) * GRID_SIZE);
int y = (int)(Math.round(pPoint.y() / GRID_SIZE) * GRID_SIZE);
int x = (int)(GeomUtils.round(pPoint.x() / GRID_SIZE) * GRID_SIZE);
int y = (int)(GeomUtils.round(pPoint.y() / GRID_SIZE) * GRID_SIZE);
return new Point(x, y);
}

Expand Down
13 changes: 7 additions & 6 deletions src/org/jetuml/rendering/edges/ArrowHeadRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import static org.jetuml.rendering.ArrowHead.V;

import org.jetuml.geom.Conversions;
import org.jetuml.geom.GeomUtils;
import org.jetuml.geom.Line;
import org.jetuml.geom.Point;
import org.jetuml.geom.Rectangle;
Expand Down Expand Up @@ -117,10 +118,10 @@ private static Path getPath(ArrowHead pArrowHead, Line pAxis)
int dx = pAxis.getX2() - pAxis.getX1();
int dy = pAxis.getY2() - pAxis.getY1();
final double angle = Math.atan2(dy, dx);
int x1 = (int) Math.round(pAxis.getX2() - ARROW_LENGTH * Math.cos(angle + ARROW_ANGLE));
int y1 = (int) Math.round(pAxis.getY2() - ARROW_LENGTH * Math.sin(angle + ARROW_ANGLE));
int x2 = (int) Math.round(pAxis.getX2() - ARROW_LENGTH * Math.cos(angle - ARROW_ANGLE));
int y2 = (int) Math.round(pAxis.getY2() - ARROW_LENGTH * Math.sin(angle - ARROW_ANGLE));
int x1 = GeomUtils.round(pAxis.getX2() - ARROW_LENGTH * Math.cos(angle + ARROW_ANGLE));
int y1 = GeomUtils.round(pAxis.getY2() - ARROW_LENGTH * Math.sin(angle + ARROW_ANGLE));
int x2 = GeomUtils.round(pAxis.getX2() - ARROW_LENGTH * Math.cos(angle - ARROW_ANGLE));
int y2 = GeomUtils.round(pAxis.getY2() - ARROW_LENGTH * Math.sin(angle - ARROW_ANGLE));

MoveTo moveToOrigin = new MoveTo(pAxis.getX2(), pAxis.getY2());
LineTo lineTo1 = new LineTo(x1, y1);
Expand All @@ -140,8 +141,8 @@ else if(pArrowHead.isTriangle())
}
else if(pArrowHead.isDiamond())
{
final int x3 = (int) Math.round( x2 - ARROW_LENGTH * Math.cos(angle + ARROW_ANGLE));
final int y3 = (int) Math.round( y2 - ARROW_LENGTH * Math.sin(angle + ARROW_ANGLE));
final int x3 = GeomUtils.round( x2 - ARROW_LENGTH * Math.cos(angle + ARROW_ANGLE));
final int y3 = GeomUtils.round( y2 - ARROW_LENGTH * Math.sin(angle + ARROW_ANGLE));
LineTo lineTo5 = new LineTo(x3, y3);
LineTo lineTo6 = new LineTo(x2, y2);
LineTo lineTo7 = new LineTo(moveToOrigin.getX(), moveToOrigin.getY());
Expand Down
3 changes: 2 additions & 1 deletion src/org/jetuml/rendering/edges/NodeIndex.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
*******************************************************************************/
package org.jetuml.rendering.edges;

import org.jetuml.geom.GeomUtils;
import org.jetuml.geom.Line;
import org.jetuml.geom.Point;
import org.jetuml.geom.Side;
Expand Down Expand Up @@ -101,7 +102,7 @@ private static int spaceBetweenConnectionPoints(Line pNodeFace, Side pAttachment
numberOfSpaces = NUM_SPACES_EW;
}

int unadjustedSpace = Math.round((lengthOfSide - MARGIN * 2) / (float) numberOfSpaces);
int unadjustedSpace = GeomUtils.round((lengthOfSide - MARGIN * 2) / (float) numberOfSpaces);
// Closest further multiple of 10 at least 10
int result = Math.max(SPACE_INCREMENT, unadjustedSpace / SPACE_INCREMENT * SPACE_INCREMENT);
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.jetuml.geom.Conversions;
import org.jetuml.geom.Dimension;
import org.jetuml.geom.Direction;
import org.jetuml.geom.GeomUtils;
import org.jetuml.geom.Line;
import org.jetuml.geom.Point;
import org.jetuml.geom.Rectangle;
Expand Down Expand Up @@ -124,8 +125,8 @@ private void drawLabel(StateTransitionEdge pEdge, GraphicsContext pGraphics)
{
String label = wrapLabel(pEdge);
Rectangle2D labelBounds = getLabelBounds(pEdge);
Rectangle drawingRectangle = new Rectangle((int) Math.round(labelBounds.getMinX()), (int) Math.round(labelBounds.getMinY()),
(int) Math.round(labelBounds.getWidth()), (int) Math.round(labelBounds.getHeight()));
Rectangle drawingRectangle = new Rectangle(GeomUtils.round(labelBounds.getMinX()), GeomUtils.round(labelBounds.getMinY()),
GeomUtils.round(labelBounds.getWidth()), GeomUtils.round(labelBounds.getHeight()));

STRING_VIEWER.draw(label, pGraphics, drawingRectangle);
}
Expand Down
4 changes: 2 additions & 2 deletions test/org/jetuml/geom/TestDirection.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ public class TestDirection
@MethodSource("angleGenerator")
void testAllAngles(int pAngle)
{
int x = (int) Math.round(sin(Math.toRadians(pAngle))*100);
int y = (int) -Math.round(cos(Math.toRadians(pAngle))*100);
int x = GeomUtils.round(sin(Math.toRadians(pAngle))*100);
int y = -GeomUtils.round(cos(Math.toRadians(pAngle))*100);
Direction direction = Direction.fromLine(new Point(0,0), new Point(x,y));
assertEquals(pAngle % 360, direction.asAngle());
assertEquals(sin(Math.toRadians(pAngle)), getX(direction), 0.000000001);
Expand Down

0 comments on commit ec6e82e

Please sign in to comment.