convert circle into heart 2d android [closed]

為{幸葍}努か 提交于 2019-12-06 16:30:05

问题


hi sorry but my maths and physics is too weak so i tried so many times but every time i failed i need your help to complete my app plz convert this circle to heart

import android.graphics.Bitmap;
public class Circle {
float origRadius,deltaRadius,radius,origX,deltaX,x,origY,deltaY,y;
int color,alpha,steps,currentStep;
Bitmap bitmap;

public Circle(float xCenter, float yCenter, float radius,
        int color, int steps) {
    this.x = xCenter;
    this.origX = xCenter;
    this.deltaX = (float) (40.0 * Math.random() - 20.0);

    this.y = yCenter;
    this.origY = yCenter;
    this.deltaY = (float) (40.0 * Math.random() - 20.0);

    this.origRadius = radius;
    this.radius = radius;
    this.deltaRadius = 0.5f * radius;

    this.color = color;
    this.alpha = 0;

    this.steps = steps;
}

void tick() {
    this.currentStep++;

    float fraction = (float) this.currentStep / (float) this.steps;

    this.radius = this.origRadius + fraction * this.deltaRadius;
    this.x = this.origX + fraction * this.deltaX;
    this.y = this.origY + fraction * this.deltaY;

    if (fraction <= 0.25f) {
        this.alpha = (int) (128 * 4.0f * fraction);
    } else {
        this.alpha = (int) (-128 * (fraction - 1) / 0.75f);
    }
}

boolean isDone() {
    return this.currentStep > this.steps;
}
}    

thanks in advance


回答1:


MathWorld had a great heart shaped function; http://mathworld.wolfram.com/HeartCurve.html

Basically you have to do something like this in your code;

float fraction = (float) this.currentStep / (float) this.steps;

-->

float t = this.currentStep * 2.0 * Math.PI / (float) this.steps;

this.x = 16.0 * Math.pow(Math.sin(t), 3.0));
this.y = 13.0 * Math.cos(t) - 5.0 * Math.cos(2.0 * t) -
          2.0 * Math.cos(3.0 * t) - Math.cos(4.0 * t);

Hope this helps, I'm writing this blindly so bear with me if there's some mistakes. For radius you might want to do something like this;

this.x *= radius / 16.0;
this.y *= radius / 16.0;


来源:https://stackoverflow.com/questions/14324429/convert-circle-into-heart-2d-android

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!