Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8026697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T23:34:29+00:00 2026-06-04T23:34:29+00:00

I’m writing a sprite like simulation where I need to move a label with

  • 0

I’m writing a sprite like simulation where I need to move a label with an image in a circular path. I have 4 starting points; west, north, east and south. The label should move in a clockwise manner.

This is what I wrote and it works well, except for the fact that north and south positions circle counter-clockwise. The reason is that, I’ve used a separate trig formula to handle directions 2 and 4.

    int xstart = 450 
    int ystart = 325;
    int h = 0;
    double theta = -0.1047;

    for (int p = 0;; p++)
    {
        int xocir = xstart;
        int yocir = ystart;
        int pocir = 0; // perpendicular and base of outer circle
        int bocir = 0;

        if (direction == 1)//west - clockwise
        {
            pocir = (int) (Math.cos(theta) * (h + 300));
            bocir = (int) (Math.sin(theta) * (h + 300));
        }
        else if (direction == 2)//north - counter-clockwise
        {
            pocir = (int) (Math.sin(theta) * (h + 300));//reversed sin,cos
            bocir = (int) (Math.cos(theta) * (h + 300));
        }
        else if (direction == 3)//east - clockwise
        {
            pocir = (int) (Math.cos(theta) * (h - 300));
            bocir = (int) (Math.sin(theta) * (h - 300));
        }
        else if (direction == 4)//south - counter-clockwise
        {
            pocir = (int) (Math.sin(theta) * (h - 300));
            bocir = (int) (Math.cos(theta) * (h - 300));
        }

        xocir = xocir - pocir;
        yocir = yocir - bocir;
        theta = theta - 0.005;

        setBounds(xocir, yocir, 65, 65);
            }

Is there a more efficient way to tackle this? Maybe a simpler trig method. And what can I do to make sure all motion is clockwise?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T23:34:30+00:00Added an answer on June 4, 2026 at 11:34 pm

    Just use one of the clockwise if statements instead of a different equation depending on direction. The start position is controlled by the initial value of theta and the parametric equations used.

    Let
    x = circleCenterX - (int) (Math.sin(theta) * radius),
    y = circleCenterY - (int) (Math.cos(theta) * radius),
    circleCenterX = 450,
    circleCenterY = 325, and
    radius = 300.

    If theta = 0 deg, then
    (x,y) = (450 - sin(0) * 300, 325 - cos(0) * 300)
          = (450, 25) which is north of (450, 325)
    
    If theta = 90 deg, then
    (x,y) = (450 - sin(90) * 300, 325 - cos(90) * 300)
          = (150, 325) which is west of (450, 325)
    
    If theta = 180 deg, then
    (x,y) = (450 - sin(180) * 300, 325 - cos(180) * 300)
          = (450, 625) which is south of (450, 325)
    
    If theta = 270 deg, then
    (x,y) = (450 - sin(270) * 300, 325 - cos(270) * 300)
          = (750, 325) which is east of (450, 325)
    

    The following class encapsulates this idea:

    import java.awt.Dimension;
    import java.util.Timer;
    import java.util.TimerTask;
    
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.SwingUtilities;
    
    public class RotatingLabel extends JLabel {
        private static final long serialVersionUID = 474652899660954020L;
        private final int circleCenterX;
        private final int circleCenterY;
        private final int radius;
        private double theta;
        private final double thetaIncrement;
    
        public RotatingLabel(String text, //
                int circleCenterX, int circleCenterY, int radius, //
                double initialTheta, double thetaIncrement) {
            super(text);
            this.circleCenterX = circleCenterX;
            this.circleCenterY = circleCenterY;
            this.radius = radius;
            this.theta = initialTheta;
            this.thetaIncrement = thetaIncrement;
            rotate();
        }
    
        public void rotate() {
            setBounds( //
                    this.circleCenterX - (int) (Math.sin(this.theta) * this.radius), //
                    this.circleCenterY - (int) (Math.cos(this.theta) * this.radius), //
                    (int) getPreferredSize().getWidth(), (int) getPreferredSize().getHeight());
            this.theta -= this.thetaIncrement;
        }
    
        public static void main(String[] args) {
            double initialTheta = Math.toRadians(0); // start north
            // double initialTheta = Math.toRadians(90); // start west
            // double initialTheta = Math.toRadians(180); // start south
            // double initialTheta = Math.toRadians(270); // start east
    
            final RotatingLabel label = new RotatingLabel("Foo Bar", //
                    450, // circleCenterX
                    325, // circleCenterY
                    300, // radius
                    initialTheta, //
                    0.005); // thetaIncrement
    
            JFrame frame = new JFrame();
            frame.setSize(new Dimension(800, 700));
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(label);
            frame.setVisible(true);
    
            Timer timer = new Timer();
            timer.scheduleAtFixedRate(new TimerTask() {
                @Override
                public void run() {
                    SwingUtilities.invokeLater(new Runnable() {
                        @Override
                        public void run() {
                            label.rotate();
                        }
                    });
                }
            }, 0, 1000l / 60l); // 60 frames per second
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have thousands of HTML files to process using Groovy/Java and I need to
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.