Android 2D graphics (circle, straight line, point) tool class (continuous update)

Source: Internet
Author: User

Android 2D graphics (circle, straight line, point) tool class (continuous update)


public class Circle {private PointF centerPoint;private float radius;public PointF getCenterPoint() {return centerPoint;}public void setCenterPoint(PointF centerPoint) {this.centerPoint = centerPoint;}public float getRadius() {return radius;}public void setRadius(float radius) {this.radius = radius;}}



Public class CircleUtils {/*** calculate the coordinates and radius of the center of the Circle based on the three points on the Circle * @ param pA * @ param pB * @ param pC * @ return */public static Circle getCircle (PointF pA, pointF pB, PointF pC) {float mat1, mat2, mat3; mat1 = (pB. x * pB. x + pB. y * pB. y)-(pA. x * pA. x + pA. y * pA. y) * (2 * (pC. y-pA.y)-(pC. x * pC. x + pC. y * pC. y)-(pA. x * pA. x + pA. y * pA. y) * (2 * (pB. y-pA.y); mat2 = (2 * (pB. x-pA.x) * (pC. x * pC. x + pC. y * pC. y)-(pA. x * pA. x + pA. y * pA. y)-(2 * (pC. x-pA.x) * (pB. x * pB. x + pB. y * pB. y)-(pA. x * pA. x + pA. y * pA. y); mat3 = 4 * (pB. x-pA.x) * (pC. y-pA.y)-(pC. x-pA.x) * (pB. y-pA.y); Circle circle = new Circle (); PointF centerPoint = new PointF (); float radius; centerPoint. x = mat1/mat3; centerPoint. y = mat2/mat3; radius = (float) Math. sqrt (pA. x-centerPoint.x. x-centerPoint.x) +. y-centerPoint.y. y-centerPoint.y); circle. setCenterPoint (centerPoint); circle. setRadius (radius); return circle ;} /*** obtain the coordinates of another point on both ends of an arc * @ param circle * @ param startP the point on one end of the arc * @ param angle of the arc * @ return */public PointF getEndPointOfArc (Circle circle, pointF startP, float angle) {PointF centerP = circle. getCenterPoint (); PointF endPointF = new PointF (); endPointF. x = (float) (centerP. x + (startP. x-centerP.x) * Math. cos (angle * Math. PI/180)-(startP. y-centerP.y) * Math. sin (angle * Math. PI/180); endPointF. y = (float) (centerP. y + (startP. x-centerP.x) * Math. sin (angle * Math. PI/180) + (startP. y-centerP.y) * Math. cos (angle * Math. PI/180); return endPointF ;}}






Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.