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

  • Home
  • SEARCH
  • 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 536083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:47:05+00:00 2026-05-13T09:47:05+00:00

I develop a Python-based drawing program, Whyteboard ( https://launchpad.net/whyteboard ) I’m developing features to

  • 0

I develop a Python-based drawing program, Whyteboard (https://launchpad.net/whyteboard)

I’m developing features to allow the user to rotate and scale a polygon that they draw. Here’s my problem:

I have a Polygon class containing a list of all points, which is “closed off” at the end. Users can select drawn shapes in my program, which “highlights” them, drawing selection handles at each point. These points can be “grabbed” to change its position, and altering the polygon’s shape.

I have a problem: I need to figure out how to calculate a the resizing “scale” to apply to the polygon. For example, (with the mouse held down), the user moving their mouse away from the shape should be a “grow” action, and bringing the mouse towards the shape should shrink it.

I have code in place to perform the scale (which I believe is correct) but I just can’t create a “good” scaling factor. The code below is what I’ve come up with, based on the answers

/edit — here is the solved code.

def rescale(self, x, y):
    """ 
    x and y are the current mouse positions. the center and "original" mouse 
    coords are calculated below
    """
    if not self.center:
        a = sum([x for x, y in self.points]) / len(self.points)
        b = sum([y for x, y in self.points]) / len(self.points)
        self.center = (a, b)
    if not self.orig_click:  # where the user first clicked on
        self.orig_click = (x, y)
    if not self.original_points:  # the points before applying any scaling
        self.original_points = list(self.points)


    orig_click = self.orig_click
    original_distance = math.sqrt((orig_click[0] - self.center[0]) ** 2 + (orig_click[1] - self.center[1]) ** 2)

    current_distance = (math.sqrt((x - self.center[0]) ** 2 + 
                       (y - self.center[1]) ** 2))
    self.scale_factor = current_distance / original_distance        

    for count, point in enumerate(self.original_points): 
        dist = (point[0] - self.center[0], point[1] - self.center[1]) 
        self.points[count] = (self.scale_factor * dist[0] + self.center[0], self.scale_factor * dist[1] + self.center[1]) 

Currently this code seems to scale my polygon down to nothing quickly, and no amount of mouse movement will grow it back. Sometimes it will do the opposite, and grow quickly; but not shrink back.

  • 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-05-13T09:47:05+00:00Added an answer on May 13, 2026 at 9:47 am

    First, let’s correct your scaling code:

    for count, point in enumerate(self.points): 
        dist = (point[0] - self.center[0], point[1] - self.center[1]) 
        self.points[count] = (self.scale_factor * dist[0] + self.center[0], self.scale_factor * dist[1] + self.center[1]) 
    

    I hope your points are kept in floating point, because integer truncation errors are going to accumulate very quickly. It might be better to have two copies of the points, one scaled and one unscaled.

    To determine the scale factor, take the ratio of the distance from the original click to the center, and the current mouse position to the center.

    original_distance = sqrt((click[0] - self.center[0])**2 + (click[1] - self.center[1])**2)
    current_distance = sqrt((current_position[0] - self.center[0])**2 + (current_position[1] - self.center[1])**2)
    self.scale_factor = current_distance / original_distance
    

    Edit: Your latest problem emphasizes the importance of having two sets of points, the original and the scaled. Since the scale factor is relative to the original size of the shape, you need to start with the original points of the shape each time you scale. You can consolidate that back down to one set when the user is done playing with the mouse.

    And to your comment, no you don’t have to recalculate the center. The center should not be moving.

    Edit 2: When you scale, you’re scaling from one size to another size. If you’re rescaling constantly, you have two choices: keep one copy of the shape at its original size, or make your scale factor relative to the last size of the shape, rather than the original size. I prefer the two copy approach, because otherwise it’s too easy for errors to accumulate, even if you’re using floating point; it’s also easier to get the logic right.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I develop a Python-based drawing program, Whyteboard . I have tools that the user
I have program that requires Python 3, but I develop Django and it uses
I am to develop a Python-based Application for people with motion problems. I have
I'm currently developing a web app on Django/Python, and I consider moving to ASP.NET
I have created a project with iron python and .net under sharp develop but
Can python be used as a language to develop browser based games? Like we
I want to learn C++ so that i can develop C++ Python modules for
In python how can i develop this algorithm to find common patterns in a
In a Python system for which I develop, we usually have this module structure.
I develop open-source project in Python and I want it to be easy accessible

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.