Creating suggestions - code
Creating suggestions
One of the main features of the app is to give suggestions. When you tap on the screen to create points, suggestions will appear on top. For example, if I created two points the suggestions will contain a circle, semi-circle and a straight line.
![]() |
Suggestions for two points |
How suggestions are drawn - Java code
First, create a rectangle with the points. This is obtained by finding the top left and bottom right points.
public static Rect clipRectFrom(Point... points) { int topX = Integer.MAX_VALUE, topY = Integer.MAX_VALUE, rightX = 0, rightY = 0; for (Point point : points) { if (topX > point.x) { topX = point.x; } else if (rightX < point.x) { rightX = point.x; } if (topY > point.y) { topY = point.y; } else if (rightY < point.y) { rightY = point.y; } } return new Rect(topX, topY, rightX, rightY); } |
int side = Math.max(rect.width(), rect.height()); Rect square = new Rect(rect.left, rect.top, rect.left + side, rect.top + side); |
Then, move the points to top left with respect to the top and left of the obtained square.
Point[] shiftedPoints = new Point[points.length]; for (int i = 0; i < points.length; i++) { Point shiftedPoint = new Point(points[i]); shiftedPoint.offset(-squareContainer.left, -squareContainer.top); shiftedPoints[i] = shiftedPoint; } |
I will explain how different types of suggestions are generated in next post.
If you are interested in my drawing app, please check it here in Google play. store
Comments
Post a Comment