draw2d根据两点位置画圆弧

 可以说圆的中心是(x0,y0),圆弧包含您的两个点(x1,y1)和(x2,y2)。 然后半径为:r = sqrt((x1-x0)(x1-x0)+(y1-y0)(y1-y0))。

一、已知两点画圆弧

	public void drawArc(Point p1,Point p2,Object object) {
		int xCenter = p1.x;
		if(p1.x > p2.x)
			xCenter = p2.x + ((p1.x-p2.x)/2);
		else if(p1.x < p2.x)
			xCenter = p1.x + ((p2.x-p1.x)/2);
		
		int yCenter= p1.y;
		if(p1.y > p2.y)
		        yCenter = p1.y - ((p1.y-p2.y)/2);
		else if(p1.y < p2.y)
		        yCenter = p2.y - ((p2.y-p1.y)/2);
		int diameter =(int) Math.sqrt(Math.pow((p1.x-p2.x),2)+Math.pow((p1.y-p2.y),2));
		int r = diameter/2;
		int x = xCenter-r;
		int y = yCenter-r;
		int width = 2*r;
		int heigh= 2*r;
		int startAngle = (int) ((180/Math.PI)*Math.atan2(p1.y-yCenter, p1.x-xCenter));
		int endAngle = (int) ((180/Math.PI)*Math.atan2(p2.y-yCenter, p2.x-xCenter));
		int arcLength = endAngle-startAngle;
		if (object instanceof Path) {
			((Path) object).addArc(x, y, width, heigh, startAngle, arcLength );
		}else if (object instanceof Graphics) {
			((Graphics) object).fillArc(x, y, width, heigh, startAngle, arcLength);
		}
	}

二、已知两端点和圆点画圆弧

	public void drawArc(Point p1,Point p2,Object object,int xCenter,int yCenter) {

		int diameter =(int) Math.sqrt(Math.pow((p1.x-p2.x),2)+Math.pow((p1.y-p2.y),2));
		int r = diameter/2;
		int x = xCenter-r;
		int y = yCenter-r;
		int width = 2*r;
		int heigh= 2*r;
		int startAngle = (int) ((180/Math.PI)*Math.atan2(p1.y-yCenter, p1.x-xCenter));
		int endAngle = (int) ((180/Math.PI)*Math.atan2(p2.y-yCenter, p2.x-xCenter));
		((Graphics) object).fillArc(x, y, width, heigh, startAngle, endAngle);
		
	}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值