I need to know how to do the this assignment.

moveTo(shape, newCenter) shape is a graphics object that supports the getCenter method and the newCenter is a Point. Moves shape so that newCenter is its center.

Use your function to write a program that draws a circle and then allows the user to click the window 10 times. Each time the user clicks, the circle is moved where the user clicked.

The window needs to be 600x600.


8 Years
Discussion Span
Last Post by Ene Uran

I think you need to go for Vpython,
www.vpython.org . Myself I have not learned it but I tried veeery little
here is a code i took from examples directory of Vpy

from visual import *

dt = 0.1

g = []
for i in range(3):
  g.append(display(y = 30 + 200*i, width=600, height=170))

g[0].title="k=6N/m mass=2.0kg"
g[1].title="k=6N/m mass=1.0kg"
g[2].title="k=6N/m mass=0.5kg"

bands = [ curve( x = arange(-50,50), display=g[0], color=color.red, k = 6., mass = 2.0),
          curve( x = arange(-50,50), display=g[1], color=color.yellow, k = 6., mass = 1.0),
          curve( x = arange(-50,50), display=g[2], color=color.green, k = 6., mass = 0.5),

for band in bands:
    band.radius = 0.5
    band.momentum = zeros((100,3),Float)

    ### Uncomment exactly one of the following lines: ###

    band.momentum[:25,1] = sin(band.x[:25]*pi / 25.0)*3   # half-wave pulse
##    band.momentum[:25,1] = sin(band.x[:25]*2*pi / 25.0)*5 # full-wave pulse
##    band.momentum[:25,0] = sin(band.x[:25]*pi / 25.0)*5   # compression wave
##    band.momentum[:,1] = sin(band.x * 4 * pi / 100.0)*2   # standing wave
##    band.momentum[25,1] = 20                             # single point impulse (messy)

while 1:
    for band in bands:
        # Keep endpoints fixed:
        band.momentum[0] = band.momentum[-1] = vector(0,0,0)

        # Integrate velocity:
        band.pos = band.pos + (band.momentum/band.mass*dt)

        # force[n] is the force on point n from point n+1 (to the right):
        force = band.k * (band.pos[1:] - band.pos[:-1])

        # all points but the last experience forces to the right:
        band.momentum[:-1] = band.momentum[:-1] + force * dt

        # all points but the first experience forces to the left:
        band.momentum[1:] = band.momentum[1:] - force * dt

Your Python installation usually includes the Tkinter GUI toolkit. Tkinter can do the things you want to do.


Yes, you can use Tkinter. Here is a hopefully helpful explanation how Tkinter draws circles that follow a mouse click. The basics are there, you just have to apply them to your problem:

# draw a circle with given center (x,y) and radius at mouse click point

import Tkinter as tk

def draw_circle(event):
    draw a circle with a given radius and the mouse position center (x,y)
    x = event.x
    y = event.y
    radius = 40
    # to draw a circle you need to get the upper left
    # and lower right corner coordinates of a square
    rect = get_square(x, y, radius)
    # draw the cicle that fits into the rect/square
    circle = cv.create_oval(rect)

def get_square(x, y, radius):
    given the center=(x,y) and radius
    calculate the square for the circle to fit into
    return x1, y1, x2, y2 of square's ulc=(x1,y1) and 
    lrc=(x2,y2) diagonal corner coordinates
    x1 = x - radius
    y1 = y - radius
    x2 = x + radius
    y2 = y + radius
    return x1, y1, x2, y2

# create the basic window, let's call it 'root' 
root = tk.Tk()
root.title("click the mouse on the green area")

# create a canvas to draw on
cv = tk.Canvas(root, width=600, height=600, bg='green')
# position the canvas

# respond to the mouse as it is clicked
root.bind("<Button-1>", draw_circle)

# start the event loop
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.