Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
With what I currently have, my circle just flies away in a straight line. I'm trying to get the circle to spin around the other circle but the circular movement just isn't happening. I am very new to coding. Thank you in advance!

from graphics import *

def main():
    winName = input ("What would you like the name of the window to be? ")
    color1 = input("What color would you like the circle to be? ")
    x1, y1 = eval(input("What are the x, y coordinates of the center of the circle? "))
    radius1 = eval(input("What is the radius of the circle? "))

    win = GraphWin (winName, 500, 500)

    center1 = Point (x1, y1)
    circ1 = Circle (center1, radius1)
    circ1.setFill (color1)
    circ1.draw (win)

    color2 = ('green')
    x2 = x1
    y2 = y1 - (2 * radius1)
    radius2 = radius1 * (1/2)

    center2 = Point (x2, y2)
    circ2 = Circle (center2, radius2)
    circ2.setFill (color2)
    circ2.draw (win)

    line = Line (center1, center2)
    line.setFill('red')
    line.draw(win)

    for i in range(5):
        hyp = radius1 * 2
        leg = x2 - x1
        theta = math.acos(leg/hyp)
        circ2.move(x1 + (math.cos(theta) * (radius1 * 2)), y1 + (math.sin(theta) * (radius1 * 2)))
        line.undraw()
        time.sleep(1)

    newCenter = (circ2.getCenter(), center1)

    line = Line(circ2.getCenter(), center1)
    line.setFill('red')
    line.draw(win)

main()


What I have tried:

I have tried many different math formulas to try and get different values that could be useful but none seem to work.
Posted
Updated 21-Sep-22 20:49pm

5 values are not enough to draw a circle: they draw a rough pentagram (ish) and will only approximate a circle for tiny radii.

Change the range(5) to a higher value - 100 or so, and increase theta by 2π / 100 each time round the loop (2π radians is 360o so the angle needs to change through the whole 2π radian range for a complete circle).

The exact number or points you need to plot will depend on the radius of the circle: the bigger it is the more points you need to get a smooth surface.
 
Share this answer
 
Comments
CPallini 22-Sep-22 2:34am    
5.
You could start using simple constant values, in order to better understand what is happening.
Try, for instance
Python
from graphics import *
import math

def main():

    winName = "circles"
    win = GraphWin (winName, 500, 500)

    color1 = "red"
    x1 = 250
    y1 = 250
    radius1 = 125


    center1 = Point (x1, y1)
    circ1 = Circle (center1, radius1)
    circ1.setFill (color1)
    circ1.draw (win)

    color2 = "green"
    radius2 = radius1/2
    x2 = x1
    y2 = y1 - (radius1+radius2)

    center2 = Point (x2, y2)
    circ2 = Circle (center2, radius2)
    circ2.setFill (color2)
    circ2.draw (win)


    phi = 0
    while phi < 2*math.pi:
        nx = x1 + (radius1+radius2) * math.sin(phi)
        ny = y1 + (radius1+radius2) * math.cos(phi)
        dx = nx-x2
        dy = ny-y2
        x2 = nx
        y2 = ny
        circ2.move(dx,dy)
        phi = phi+math.pi/100
        time.sleep(.1)

main()
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900