Draw a Heart Shape on Android with UiAutomator Math Functions

This tutorial shows how to use the polar equation of a heart, convert mathematical coordinates to screen coordinates, and employ UiAutomator's swipe method to draw a heart shape on an Android device using Java.

FunTester
FunTester
FunTester
Draw a Heart Shape on Android with UiAutomator Math Functions

The author wanted to create a heart-shaped pattern on an Android device using UiAutomator's math utilities and share the solution for feedback.

By applying the polar equation of a heart, the method first computes a series of angles, then converts the mathematical (x, y) coordinates to screen coordinates because the device's coordinate system differs from the standard Cartesian system.

Key parameters are x and y for the heart's center and r for its radius. The implementation follows these steps:

Calculate the angle increment d = Math.PI / 30 and fill an angle array of 61 values.

Compute the horizontal offsets ox using r * (2 * Math.cos(angle[i]) - Math.cos(2 * angle[i])).

Compute the vertical offsets oy using r * (2 * Math.sin(angle[i]) - Math.sin(2 * angle[i])).

Create a Point[] heart array, translate each offset to screen coordinates ( heart[i].x = (int) oy[i] + x, heart[i].y = -(int) ox[i] + y), and store the points.

Finally, invoke getUiDevice().swipe(heart, 2) to draw the heart on the screen.

public void heart(int x, int y, int r) {
    double d = (double) (Math.PI / 30);
    double[] angle = new double[61]; // set angle step
    for (int i = 0; i < 61; i++) {
        angle[i] = i * d;
    }
    double[] ox = new double[61];
    for (int i = 0; i < 61; i++) {
        ox[i] = r * (2 * Math.cos(angle[i]) - Math.cos(2 * angle[i]));
    }
    double[] oy = new double[61];
    for (int i = 0; i < 61; i++) {
        oy[i] = r * (2 * Math.sin(angle[i]) - Math.sin(2 * angle[i]));
    }
    Point[] heart = new Point[61];
    for (int i = 0; i < 61; i++) {
        heart[i] = new Point();
        heart[i].x = (int) oy[i] + x;
        heart[i].y = -(int) ox[i] + y;
    }
    getUiDevice().swipe(heart, 2);
}

This method is similar to drawing a circle but modifies the mathematical formula and rotates the coordinates to produce a heart shape.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaGraphicsAndroidAutomationUIAutomator
FunTester
Written by

FunTester

10k followers, 1k articles | completely useless

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.