当前位置: 代码迷 >> J2SE >> 求大神帮小弟我看看这个DEMO的createCircle方法,如何返回圆的点阵的
  详细解决方案

求大神帮小弟我看看这个DEMO的createCircle方法,如何返回圆的点阵的

热度:648   发布时间:2013-02-25 00:00:00.0
求大神帮我看看这个DEMO的createCircle方法,怎么返回圆的点阵的
如题↑↑↑
Java code
package eclipsecookbook;import org.eclipse.swt.SWT;import org.eclipse.swt.graphics.Region;import org.eclipse.swt.widgets.Button;import org.eclipse.swt.widgets.Display;import org.eclipse.swt.widgets.Event;import org.eclipse.swt.widgets.Listener;import org.eclipse.swt.widgets.Shell;public class NonRectanglegularClass {        public static void main(String[] args) {        final Display display = new Display();        final Shell shell = new Shell(display, SWT.NO_TRIM);        Region region = new Region();        region.add(createCircle(50, 50, 50));        region.subtract(createCircle(50, 50, 20));        shell.setRegion(region);        shell.setSize(region.getBounds().width, region.getBounds().height);        shell.setBackground(display.getSystemColor(SWT.COLOR_BLUE));        Button button = new Button(shell, SWT.PUSH);        button.setText("Exit");        button.setBounds(35, 6, 35, 20);        button.addListener(SWT.Selection, new Listener() {            @Override            public void handleEvent(Event arg0) {                shell.close();            }        });        shell.open();        while (!shell.isDisposed()) {            if (!display.readAndDispatch()) {                display.sleep();            }        }        display.dispose();    }    static int[] createCircle(int xOffset, int yOffset, int radius) {        int[] circlePoints = new int[10 * radius];        for (int loopIndex = 0; loopIndex < 2* radius + 1; loopIndex++) {            int xCurrent = loopIndex - radius;            int yCurrent = (int) Math.sqrt(radius * radius - xCurrent                    * xCurrent);            int doubleLoopIndex = 2 * loopIndex;            circlePoints[doubleLoopIndex] = xCurrent + xOffset;            circlePoints[doubleLoopIndex + 1] = yCurrent + yOffset;            circlePoints[10 * radius - doubleLoopIndex - 2] = xCurrent                    + xOffset;            circlePoints[10 * radius - doubleLoopIndex - 1] = -yCurrent                    + yOffset;        }        return circlePoints;    }}


------解决方案--------------------------------------------------------
你已经返回一个数组点阵了,循环取出来就行了啊
------解决方案--------------------------------------------------------
楼主你想问的是,程序是如何计算出点阵的么?

是用直角三角计算公式来算的:
for (int loopIndex = 0; loopIndex < 2* radius + 1; loopIndex++) { // 注意它循环到半径×2
int xCurrent = loopIndex - radius; // 得到x坐标,结合循环起至条件,相当于从 (圆心-半径) --> (圆心+半径)
int yCurrent = int (int) Math.sqrt(radius * radius - xCurrent * xCurrent); // 直角三角形,已知斜边和直角边的边长,求另一个直角边


这种计算过程,每次实际上一个x坐标会计算出两个y坐标(以圆心所在Y轴的轴对称上),所以后续赋值的时候,是两个点:
circlePoints[doubleLoopIndex] = xCurrent + xOffset;
circlePoints[doubleLoopIndex + 1] = yCurrent + yOffset; // 这个是正的
circlePoints[10 * radius - doubleLoopIndex - 2] = xCurrent + xOffset;
circlePoints[10 * radius - doubleLoopIndex - 1] = -yCurrent + yOffset; // 这个是负的(对称)


楼主可以把 yOffset 和 xOffset 先设置为0,也即圆心就是原点,然后简化这个过程来想一想。
  相关解决方案