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);
		
	}

你可能感兴趣的:(draw2d,SWT,java,开发语言)