贝塞尔曲线

维基百科上的解释:

三角函数将直角三角形的内角和它的两个边的比值相关联,也可以等价地用与单位圆有关的各种线段的长度来定义。常见的三角函数包括正弦函数 余弦函数正切函数

贝塞尔曲线_第1张图片
Image.png

如图,在直角三角形中仅有锐角(大小在0到90度之间的角)三角函数的定义
。给定一个锐角⍬,可以做出一个直角三角形,使得其中的一个内角是⍬。设这个三角形中,⍬的对边、邻边和斜边长度分别是a,b,h,那么

⍬的正弦是对边与斜边的比值:

⍬的余弦是邻边与斜边的比值:

⍬的正切是对边与邻边的比值:

⍬的余切是邻边与对边的比值:

⍬的正割是斜边与邻边的比值:

⍬的余割是斜边与对边的比值:

医生讲解的非常详细:mask一下
http://www.jianshu.com/p/55c721887568

自己的小,有些地方带优化。

public class PushBezierView extends View implements View.OnTouchListener{
    private Path mPath;
    private Paint circlePaint;
    private Paint pathPaint;
    int width,height;
    float setPointX = 80,setPointY = 80;
    float movePointX = 100,movePointY = 100;
    int moveRadius = 40;
    int originRadius = 40;
    float previousRelativeDistance;
    public PushBezierView(Context context) {
        this(context,null);
    }
    public PushBezierView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }
    public PushBezierView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }
    private void init() {
        circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        circlePaint.setColor(Color.RED);
        circlePaint.setStyle(Paint.Style.FILL);
        pathPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        pathPaint.setColor(Color.RED);
        pathPaint.setStyle(Paint.Style.FILL);
        mPath = new Path();
        setOnTouchListener(this);
    }
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        width = w;
        height = h;
        setPointX = width / 2;
        setPointY = height / 4;
    }
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (originRadius < 0){
            canvas.drawCircle(movePointX, movePointY, moveRadius, circlePaint);
        }else {
            float dx = movePointX - setPointX;
            float dy = movePointY - setPointY;
            float relativeDistance = Math.abs(Math.max(dx, dy));
            if (relativeDistance > previousRelativeDistance) {
                originRadius = originRadius - (int) (relativeDistance / 50);
            } else {
                originRadius = originRadius + (int) (relativeDistance / 50);
            }
            canvas.drawCircle(setPointX, setPointY, originRadius, circlePaint);
            canvas.drawCircle(movePointX, movePointY, moveRadius, circlePaint);
            mPath.reset();
            float controlX = (movePointX + setPointX) / 2;
            float controlY = (movePointY + setPointY) / 2;
            double q = Math.atan(dx / dy);
            double absoluteX = Math.cos(q) * moveRadius;
            double absoluteY = Math.sin(q) * moveRadius;
            double originAbsoluteX = Math.cos(q) * originRadius;
            double originAbsoluteY = Math.sin(q) * originRadius;
            float firstX = setPointX - (float) originAbsoluteX;
            float firstY = setPointY + (float) originAbsoluteY;
            mPath.moveTo(firstX, firstY);
            float secondX = movePointX - (float) absoluteX;
            float secondY = movePointY + (float) absoluteY;
            mPath.quadTo(controlX, controlY, secondX, secondY);
            float thridX = movePointX + (float) absoluteX;
            float thridY = movePointY - (float) absoluteY;
            mPath.lineTo(thridX, thridY);
            float fourX = setPointX + (float) originAbsoluteX;
            float fourY = setPointY - (float) originAbsoluteY;
            mPath.quadTo(controlX, controlY, fourX, fourY);
            mPath.close();
            canvas.drawPath(mPath, pathPaint);
            previousRelativeDistance = relativeDistance;
        }
    }
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_MOVE:
                if (moveRadius > 0) {
                    movePointX = event.getX();
                    movePointY = event.getY();
                    invalidate();
                }
                break;
            case MotionEvent.ACTION_UP:
                if (originRadius < 0 && moveRadius > 0){
                    ValueAnimator valueAnimator = ValueAnimator.ofInt(40,0);
                    valueAnimator.setInterpolator(new DecelerateInterpolator());
                    valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                        @Override
                        public void onAnimationUpdate(ValueAnimator animation) {
                            moveRadius = (int)animation.getAnimatedValue();
                            invalidate();
                        }
                    });
                    valueAnimator.start();
                }
        }
        return true;
    }
}

你可能感兴趣的:(贝塞尔曲线)