draw2d在圆上画刻度

draw2d在圆上画刻度


在圆上画刻度首先需要知道圆每个点的位置:

方法一:

Math.sin(x) x 的正玄值。返回值在 -1.0 到 1.0 之间;

Math.cos(x) x 的余弦值。返回的是 -1.0 到 1.0 之间的数;

这两个函数中的X 都是指的“弧度”而非“角度”   
弧度的计算公式为: 角度*(PI/180);  (角度转弧度可参考:角度与弧度互转)
30° 角度 的弧度 = 30 * (PI/180)

如何得到圆上每个点的坐标?

解决思路:根据三角形的正玄、余弦来得值;

假设一个圆的圆心坐标是(a,b),半径为r,

则圆上每个点的:
X坐标=a + Math.sin(角度 * (Math.PI / 180)) * r ;
Y坐标=b + Math.cos(角数 * (Math.PI / 180)) * r ;

注意:
1、以“12点为起点, 角度顺时针增大“,求X坐标和Y坐标的方法是:
X坐标=a + Math.sin(角度 * (Math.PI / 180)) * r ;
Y坐标=b - Math.cos(角数 * (Math.PI / 180)) * r ;

2、以“12点为起点, 角度逆时针增大“,求X坐标和Y坐标的方法是:
X坐标=a - Math.sin(角度 * (Math.PI / 180)) * r ;
Y坐标=b - Math.cos(角数 * (Math.PI / 180)) * r ;

3、以“6点为起点, 角度顺时针增大“,求X坐标和Y坐标的方法是:
X坐标=a - Math.sin(角度 * (Math.PI / 180)) * r ;
Y坐标=b + Math.cos(角数 * (Math.PI / 180)) * r ;

7、以“6点为起点, 角度逆时针增大“,求X坐标和Y坐标的方法是:
X坐标=a + Math.sin(角度 * (Math.PI / 180)) * r ;
Y坐标=b + Math.cos(角数 * (Math.PI / 180)) * r ;

4、以“9点为起点, 角度逆时针增大“,求X坐标和Y坐标的方法是:
X坐标=a - Math.cos(角度 * (Math.PI / 180)) * r ;
Y坐标=b + Math.sin(角数 * (Math.PI / 180)) * r ;
5、以“9点为起点, 角度顺时针增大“,求X坐标和Y坐标的方法是:
X坐标=a - Math.cos(角度 * (Math.PI / 180)) * r ;
Y坐标=b - Math.sin(角数 * (Math.PI / 180)) * r ;

6、一般“3点为起点, 角度增大时为顺时针方向“,求X坐标和Y坐标的方法是:
X坐标 = a + Math.cos(角度 * (Math.PI / 180)) * r;
Y坐标 = b + Math.sin(角度 * (Math.PI / 180)) * r;

一般“3点为起点, 角度增大时为逆时针方向“,求X坐标和Y坐标的方法是:
X坐标 = a + Math.cos(角度 * (Math.PI / 180)) * r;
Y坐标 = b - Math.sin(角度 * (Math.PI / 180)) * r;
方法二:

假设一个圆的圆心坐标是(a,b),半径为r,

则圆上每个点的X坐标=a + Math.sin(2Math.PI / 360角度) * r ;Y坐标=b + Math.cos(2Math.PI / 360角度) * r ;
30° 角度 的弧度 = 2PI/36030
X坐标=a + Math.sin(2Math.PI / 36030) * r ;Y坐标=b + Math.cos(2Math.PI / 36030) * r ;
代码实现:


import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.Ellipse;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.Polyline;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.PointList;
import org.eclipse.draw2d.geometry.Rectangle;

import com.eyun.editor.model.Meter;

/**
 * @author jg
 *
 * 2022年4月15日
 */
public class Ellipse1 extends Ellipse {
	private Meter meter;

	@Override
	public void paintFigure(Graphics graphics) {
		// TODO Auto-generated method stub
		super.paintFigure(graphics);
		//得到圆的位置
		Rectangle copy = getBounds().getCopy();
		int x = copy.x;
		int y = copy.y;
		int width = copy.width;
		int height = copy.height;
		//圆心位置
		int circleCenterX= x + (width/2);
		int circleCenterY= y + (height/2);
		//半径
		int r = height/2;
		
		//线宽
		graphics.setLineWidth(3);
//		Math.sin(x)      x 的正玄值。返回值在 -1.0 到 1.0 之间;
//		Math.cos(x)    x 的余弦值。返回的是 -1.0 到 1.0 之间的数;
		/** ***画圆形时钟的刻度,每6度便有一个刻度**** */
	    for (int i = 0; i < 360; i = i + 6) {
	    	graphics.setForegroundColor(ColorConstants.gray);
	    
	      // 画刻度
	      if (i % 90 == 0) {
	    	  //线长度
	    	  int len =15;
	        // 对于0,3,6,9点位置,使用一个大的刻度
	    	  graphics.setForegroundColor(ColorConstants.black);
	    	  graphics.drawLine(circleCenterX + (int) (Math.cos(i * Math.PI / 180) * r),
	    			  circleCenterY - (int) (Math.sin(i * Math.PI / 180) * r), 
	    			  circleCenterX + (int) (Math.cos(i * Math.PI / 180) * (r-len)) ,
	    			  circleCenterY - (int) (Math.sin(i * Math.PI / 180) * (r-len)));
	      }else {
	    	  int len =10;
	    	  graphics.setForegroundColor(ColorConstants.red);
	    	  graphics.drawLine(circleCenterX + (int) (Math.cos(i * Math.PI / 180) * r),
	    			  circleCenterY - (int) (Math.sin(i * Math.PI / 180) * r), 
	    			  circleCenterX + (int) (Math.cos(i * Math.PI / 180) * (r-len)) ,
	    			  circleCenterY - (int) (Math.sin(i * Math.PI / 180) * (r-len)));
	      }
	    }
		// 刻度
      /*  for (int i = 1; i <= 60; i++) {
            int len = 5;
            float lineWidth = 1f;
            if (i % (60 / 12) == 0) {
                len = 10;
                lineWidth = 2f;
            }
            double angle = (2 * Math.PI / 60) * i;
            Point from = new Point((circleCenterX + r * Math.sin(angle)), (circleCenterY + r * Math.cos(angle)));
            Point to = new Point((circleCenterX + (r - len) * Math.sin(angle)), (circleCenterY + (r - len) * Math.cos(angle)));
            Polyline line = new Polyline();
            line.setForegroundColor(ColorConstants.black);
            line.addPoint(from);
            line.addPoint(to);
            line.setLineWidthFloat(lineWidth);
            this.add(line);
        }*/
		
	}

	public Meter getMeter() {
		return meter;
	}

	public void setMeter(Meter meter) {
		this.meter = meter;
	}

}

效果:
在这里插入图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值