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);
    }
Then, create a square from the rectangle. The square has the minimum size to contain the rectangle fully.
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;
}
Finally, these points are scaled down by multiplying with the scale factor. The scale factor is the ratio between the size of the suggestion box (This is again a square. The top panel consists of many such squares for each suggestion) and the size of the square obtained above.

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

Popular posts from this blog

LightDraw Drawing App