计算旋转轮子落在哪里

huangapple go评论65阅读模式
英文:

Calculating what a spinning wheel lands on

问题

当我旋转轮盘时,我无法输出我所落在的正确数字(无论我将spinSpeed变量设置为多少,我似乎总是偏离1或更多)。我已经观看了一个关于如何获取指针所在数字的视频,我已经将其实现到我的代码中(包括数组和3个souts)。任何帮助都将不胜感激。

public class RoulettePanel extends javax.swing.JPanel {

    int x;
    int y;
    int rotationAngle = 0;
    Image image;
    double spinSpeed;
    double speedDeduction;
    // int[] Slices = {0,27,10,25,29,12,8,19,31,18,6,21,33,16,4,23,35,14,2,0,28,9,26,30,11,7,20,32,17,5,22,34,15,3,21,36,13,1};

    int[] Slices = {1,13,36,21,3,15,34,22,5,17,32,20,7,11,30,26,9,28,0,2,14,35,23,4,16,33,21,6,18,31,19,8,12,29,25,10,27,0};

    public RoulettePanel() {
        image = new ImageIcon("src/images/rouletteWheel.png").getImage();
        spinSpeed = 5;
        speedDeduction = 0;

        add(startTimer);
        startTimer.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                time.start();
            }
        });

    }

    JButton startTimer = new JButton("Spin wheel");

    Timer time = new Timer(10, new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            speedDeduction += 0.0001;
            spinSpeed -= speedDeduction;
            rotationAngle += spinSpeed;
            if (spinSpeed <= 0) {
                time.stop();
                System.out.println(rotationAngle);
                System.out.println(rotationAngle/(365/38));
                System.out.println(Slices[Integer.valueOf(rotationAngle/(365/38))]);
            }
            repaint();
        }

    });

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g.create();

        if (rotationAngle >= 365) {
            rotationAngle -= 365;
        }
        double rotationAngleRad = Math.toRadians(rotationAngle);
        g2d.rotate(rotationAngleRad, 600, 600);
        g2d.drawImage(image, 200, 200, null);

        int[] xPoints = {575, 600, 625};
        int[] yPoints = {200, 250, 200};
        g.setColor(Color.orange);
        g.fillPolygon(xPoints, yPoints, 3);

        g2d.dispose();
    }

}
英文:

when I spin the wheel I am unable to output the correct number that I have landed on (I always seem to get it 1 or more off depending on what i set the spinSpeed variable to). I have watched a video on how to get what number the pointer is sitting on which I have implemented into my code(the array and the 3 souts). Any help would be appreciated.

计算旋转轮子落在哪里

public class RoulettePanel extends javax.swing.JPanel {

    int x;
    int y;
    int rotationAngle = 0;
    Image image;
    double spinSpeed;
    double speedDeduction;
   // int[] Slices = {0,27,10,25,29,12,8,19,31,18,6,21,33,16,4,23,35,14,2,0,28,9,26,30,11,7,20,32,17,5,22,34,15,3,21,36,13,1};

    int[] Slices = {1,13,36,21,3,15,34,22,5,17,32,20,7,11,30,26,9,28,0,2,14,35,23,4,16,33,21,6,18,31,19,8,12,29,25,10,27,0};

    public RoulettePanel() {
        image = new ImageIcon(&quot;src/images/rouletteWheel.png&quot;).getImage();
        spinSpeed = 5;
        speedDeduction = 0;

        add(startTimer);
        startTimer.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                time.start();
            }
        });

    }

    JButton startTimer = new JButton(&quot;Spin wheel&quot;);

    Timer time = new Timer(10, new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            speedDeduction += 0.0001;
            spinSpeed -= speedDeduction;
            rotationAngle += spinSpeed;
            if (spinSpeed &lt;= 0) {
                time.stop();
                System.out.println(rotationAngle);
                System.out.println(rotationAngle/(365/38));
                System.out.println(Slices[Integer.valueOf(rotationAngle/(365/38))]);
                

            }
            repaint();
        }

    });

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g.create();

        if (rotationAngle &gt;= 365) {
            rotationAngle -= 365;
        }
        double rotationAngleRad = Math.toRadians(rotationAngle);
        g2d.rotate(rotationAngleRad, 600, 600);
        g2d.drawImage(image, 200, 200, null);

        int[] xPoints = {575, 600, 625};
        int[] yPoints = {200, 250, 200};
        g.setColor(Color.orange);
        g.fillPolygon(xPoints, yPoints, 3);

        g2d.dispose();
    }

}

答案1

得分: 1

以下是已翻译的内容:

你可以使用以下方法来获取指定角度所在的扇区。

请注意,您需要使用一个以双零开头的数组,按顺时针方向进行操作。

正如其他人已经提到的,确保使用 360,而不是 365

双零位于 _355.26 和 4.73 度之间,大约如此。
对于任何其他值,首先减去 bound,即 sector angle ÷ 2,以取消双零扇区,然后除以 sector angle,最后在末尾加上 1 以适应初始双零的取消。

/** uses [a, b) */
int index(float angle) {
    float value = 360 / 38f;
    float bound = value / 2;
    if (angle >= 360 - bound || angle < bound) return 0;
    else return (int) ((angle - bound) / value) + 1;
}

为了帮助确定它是 还是 ,你可以使用一个 enum
我使用 -1 来表示双零。

enum Sector {
    DOUBLE_ZERO, ZERO, RED, BLACK;

    static Sector number(int value) {
        return switch (value) {
            case -1 -> DOUBLE_ZERO;
            case 0 -> ZERO;
            case 1, 3, 5, 7, 9, 12, 14, 16, 18, 19, 21, 23, 25, 27, 30, 32, 34, 36 -> RED;
            case 2, 4, 6, 8, 10, 11, 13, 15, 17, 20, 22, 24, 26, 28, 29, 31, 33, 35 -> BLACK;
            default -> throw new IllegalArgumentException(String.valueOf(value));
        };
    }
}

以下是这些过程的基本实现。

int[] sectors = {
    -1, 27, 10, 25, 29, 12, 8, 19, 31, 18,
    6, 21, 33, 16, 4, 23, 35, 14, 2, 0,
    28, 9, 26, 30, 11, 7, 20, 32, 17,
    5, 22, 34, 15, 3, 21, 36, 13, 1
};
Random random = new Random();
float angle = random.nextFloat() * 360f;
int index = index(angle);
int number = sectors[index];
Sector sector = Sector.number(number);

System.out.println("angle = " + angle);
System.out.println("index = " + index);
System.out.println("number = " + number);
System.out.println("sector = " + sector.name().toLowerCase());

以下是一些输出。

angle = 198.36185
index = 21
number = 9
sector = red
angle = 18.213615
index = 2
number = 10
sector = black
angle = 3.0296087
index = 0
number = -1
sector = double_zero
英文:

You can use the following method to obtain the sector a specified angle will reside in.

Note, you'll have to use an array that begins with double-zero, working in the clockwise direction.

As others have mentioned, make sure to use 360, and not 365.

Double-zero lies within 355.26 and 4.73 &deg;, approximately.
For any other value, first subtract the bound, which is the sector angle &divide; 2, to negate the double-zero sector, then divide by the sector angle, finally adding 1 at the end to accommodate the initial double-zero negation.

/** uses [a, b) */
int index(float angle) {
    float value = 360 / 38f;
    float bound = value / 2;
    if (angle &gt;= 360 - bound || angle &lt; bound) return 0;
    else return (int) ((angle - bound) / value) + 1;
}

To aid in defining whether it is a Red or Black, you can use an enum.
I am using -1 to represent the double-zero.

enum Sector {
    DOUBLE_ZERO, ZERO, RED, BLACK;

    static Sector number(int value) {
        return switch (value) {
            case -1 -&gt; DOUBLE_ZERO;
            case 0 -&gt; ZERO;
            case 1, 3, 5, 7, 9, 12, 14, 16, 18, 19, 21, 23, 25, 27, 30, 32, 34, 36 -&gt; RED;
            case 2, 4, 6, 8, 10, 11, 13, 15, 17, 20, 22, 24, 26, 28, 29, 31, 33, 35 -&gt; BLACK;
            default -&gt; throw new IllegalArgumentException(String.valueOf(value));
        };
    }
}

Here is a basic implementation of these procedures.

int[] sectors = {
    -1, 27, 10, 25, 29, 12, 8, 19, 31, 18,
    6, 21, 33, 16, 4, 23, 35, 14, 2, 0,
    28, 9, 26, 30, 11, 7, 20, 32, 17,
    5, 22, 34, 15, 3, 21, 36, 13, 1
};
Random random = new Random();
float angle = random.nextFloat() * 360f;
int index = index(angle);
int number = sectors[index];
Sector sector = Sector.number(number);

System.out.println(&quot;angle = &quot; + angle);
System.out.println(&quot;index = &quot; + index);
System.out.println(&quot;number = &quot; + number);
System.out.println(&quot;sector = &quot; + sector.name().toLowerCase());

Here are a few outputs.

angle = 198.36185
index = 21
number = 9
sector = red
angle = 18.213615
index = 2
number = 10
sector = black
angle = 3.0296087
index = 0
number = -1
sector = double_zero

huangapple
  • 本文由 发表于 2023年5月28日 04:54:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76348994.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定