Android之2D图形(圆、直线、点)工具类 (持续更新)

本文详细探讨了在Android中如何利用2D图形API来绘制圆、直线和点。通过创建工具类,开发者可以更方便地在应用程序中实现这些基本图形的绘制,为UI设计提供了更多灵活性。文章还涵盖了图形更新和优化的技巧,适合Android开发者深入理解图形绘制原理。
摘要由CSDN通过智能技术生成


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 {

	/**
	 * 根据圆上的三个点求圆心坐标、半径
	 * @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)*(pA.x-centerPoint.x) + (pA.y-centerPoint.y)*(pA.y-centerPoint.y)));  
		 
		 circle.setCenterPoint(centerPoint);
		 circle.setRadius(radius);
		 
		 return circle;
	}
	
	/**
	 * 求一段圆弧两端另一点的坐标
	 * @param circle
	 * @param startP 圆弧一端的点
	 * @param angle 圆弧对应的角度
	 * @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;
		
	}
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值