hi how you can help me i've been trying to connect a line from a point i click to a square that have been placed there previously but have no idea how to do it, could you help please??

Recommended Answers

All 6 Replies

Tkinter, wxPython, or other?

wxpython

Well, there are several parts to this.

First, you need to be able to read and respond to the mouse click. That happens by binding your mouse events to your window.

Then, you need to specify your problem more clearly. If you want to draw a line segment from the mouse to the center of the square, then you only need to know how to draw a line segment.

But if you want to draw from the mouse to the nearest corner of the square, then you'll need to compute whichever corner is closest, and then draw.

Or if you want to draw from the mouse to the nearest *point* on the square, then you'll need to do some fancy-schmancy coordinate geometry, and then draw.

So: which problem are you trying to solve?

Jeff

draw from the mouse click to the nearest point on the square

Well sure, just pick the hardest one, then.

Here's the plan in general terms. You might want to draw it on paper.

* Divide up your canvas into nine (imaginary) sectors by extending the sides of the square. The whole thing looks like a tic-tac-toe board. Number the sectors 0 - 8.

* If the point is in sectors 0, 2, 6, or 8, then the closest point is one of the corners of the square.

* Elif the point is in sectors 1, 3, 5, or 7, then the closest point is straight down, right, left, or up (respectively).

* Else the point is inside the square, and you might have to do trial and error.

Here's a concrete example: Let the corners of your square be (100,100); (200,100); (100,200); (200,200).

Then the method above applied to the following points yields:

(5,20) --> connect to (100,100) (sector 0)
(150,20) --> connect to (150,100) (sector 1)
(300,150) --> connect to (200,150) (sector 5)
(120,175) --> try (120,100), (100,175), (120,200), (200,175). Closest fit is (120,200).

IF your squares are all aligned with sides parallel to the axes, then you can use inequalities to determine the sector:

if point.x < square.left and point.y < square.top:
   #sector 0
etc.

Hope it helps,
Jeff

Not all points in a square add up to give you 90 degrees.

' The time to wither and die is greatest at dawn, for you have everything to live for.'
Euclid

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.