I am working on using python turtle to draw a circle.when i draw the circle, mouse click inside circle erase, click anywhere in the turtle,redraw it and click again inside again erase. So the process like this :
black screen
1.Click mouse
2.Start drawing circle
3.finish
4.click inside circle
5.clear screen
6.click again anywehre in the screen redraw circle
7.click inside circle clear screen
Thanks
https://i.stack.imgur.com/LR8CH.png
import PositionService
import turtle
t=turtle.Turtle()
ts=tur
tle.Screen()
ts.bgpic("shape_window.png")
t.up()
def get_click(x,y):# get click (x,y)
#if counter == 0:
draw_circle(x,y,"green",80)# draw the circle
print("clicking at ({}, {})".format(x,y))
def draw_circle(x,y,color,rad): # draw the circle
t.goto(x,y)
t.down()
t.circle(80)
t.color("green")
t.up()
t.hideturtle()
#t.home()
def main():
#maybe use global _pen
ts.onclick(get_click) # clicker
#set_position( x,y)?
#is_visible(draw_square)
I think were from the same class. I was also struggling until I realized that they supplied a PositionService.py file. You are supposed to use the functions within that file to help.
Related
I'm exploring PowerAutomate and am stuck with a relatively simple issue;
I want to click-and-drag to draw a yellow circle in Ms paint.
Currently, my flow looks like this:
start Paint ( Run application )
Select Yellow ( Click UI Element in Window)
Click mouse
Drag?
The last two points are not working / don't know how to do.
What is the best approach?
I know it's possible to store polygons in a dictionary since they have definitive coordinates, but is there a way to store the coordinates of a circle into a dictionary and move them around? My program consists of detecting whether a mouse click is within a circle and from there, gets the coordinate of that circle moving it to wherever the user desires with another mouse click (the get.poly function, instead of moving the current circle that I already drew, makes another copy). Below is an incomplete snippet of what I want to do:
def buttonclick(x, y): # detects mouseclick
return pen.goto(x, y)
def check(ctr, pt): # check whether the click is within the circle
if (pt[0] - ctr[0])** 2 + (pt[1] - ctr[1])**2 < 5**2:
return True
if check((0,5), mouseclick coordinates): # if true, move circle to next click
# pen = the circle thats detected
# move circle coordinates to next mouseclick
# break
I tried with the code provided by /u/cdlane as follows and here is what I meant by generating a new copy
pen.goto(0,0)
pen.pd()
pen.begin_poly()
pen.circle(radius)
pen.end_poly()
shape.addcomponent(pen.get_poly(), 'red', 'black')
screen.register_shape('1', shape)
pen = Turtle(shape = '1')
pen.pu()
Function does exactly what I need it to do but using an existing circle instead of generating a new copy.
Rather than fix your implementation, I'm going to suggest a different implementation to achieve what you describe.
The circles themselves will be turtles, and clicking on one will "select" it. Clicking anywhere on the screen after that will move it. However, we don't want turtle.onclick() active at the same time as screen.onclick() since they both will get invoked, causing complications. Below is my two turtle example:
from turtle import Screen, Turtle
from functools import partial
selected = None
def on_screen_click(x, y):
global selected
screen.onclick(None)
selected.goto(x, y)
outline, fill = selected.color()
selected.color(fill, outline)
selected = None
for turtle in screen.turtles():
turtle.onclick(partial(on_turtle_click, turtle))
def on_turtle_click(self, x, y):
global selected
for turtle in screen.turtles():
turtle.onclick(None)
selected = self
outline, fill = selected.color()
selected.color(fill, outline)
# (re)enable screen onclick handler after this function, not during
screen.ontimer(partial(screen.onclick, on_screen_click))
screen = Screen()
turtle_1 = Turtle()
turtle_1.shape('circle')
turtle_1.color('red', 'black')
turtle_1.penup()
turtle_2 = turtle_1.clone()
turtle_2.goto(100, 100)
for turtle in screen.turtles():
turtle.onclick(partial(on_turtle_click, turtle))
screen.mainloop()
I'm trying to have a custom painting of a pyqtgraph derived Plot widget (the code is found here https://github.com/danielhrisca/asammdf/blob/bfa09d0b46f1b4b98b9ad1d0a0bb90cbb876b43e/asammdf/gui/widgets/plot.py#L4271): since painting a Plot with thousands of curves is very expensive I want to store the result in a QPixmap, then if I have a cursor active (implemented using an InfiteLine (see https://github.com/pyqtgraph/pyqtgraph/blob/6ed7d4fa4762aaa895f7e9b2fb09ec6df4cc793f/pyqtgraph/graphicsItems/InfiniteLine.py#L16) I would first draw the QPixmap on the viewport then on top just the cursor item. This way moving the cursor would not trigger the repaint of all the curves.
The QPixmap looks like this
and this is what happens when I create a cursor and slide it around:
Note that at the end the plot is zoomed out, this causes the reset of the QPixmap and the "native" repaint which produces the correct image with the single vertical line of the cursor.
What could be causing this?
I found the problem to be that the paint events were set to update just certain regions of the viewport instead of the full viewport.
The fix was to set the viewport update mode to full update
self.setViewportUpdateMode(QtWidgets.QGraphicsView.FullViewportUpdate)
Using Swift methods touchBegan, touchMoved and touchEnded I save the touch points and than I draw a line using UIGraphicsGetCurrentContext() with methods beginPath(), move(to: Point), addLine(to: Point) and strokePath().
This line is repeated on 4 quadrants plus their negative values, therefore 8 lines are drawn.
Here is an example:
Example image
I save this drawing as an Image when the user taps the tick (Done green button at top-right) for later manipulations.
I wonder if it's possible to create e closed path/shape with white pixels enclosed by the black lines. I want to fill the white area with custom color when the user touches inside it.
The shape is created by user input and I have no idea how it would look like.
Would be thankful to whoever finds the time to give it's contribute.
Thank you
Maybe what you want is the "Flood Fill" algorithm, please see the article.
I need help making a drawing demo.
When the user draws a line using their finger,
the line has directional arrows on both ends.
When their finger releases, it draws the line with
"?" (question-mark) in center of the line.
Then, when user taps on the "?", it will show a new view and the user can enter a
value, and the value is in that line.
And we can add multiple lines on capture-image and also we can delete
selected line.
I don't understand how can I start developing these features so
please give me an idea or any link, or suggestion to start develop this
feature.
You should use a UITapGestureRecognizer and a UIBezierPath. Have it so that person taps at one point and then taps at a second point, then make a UIBezierPath between the two points. To get the question mark in the middle you could make it so that the line goes from the first point to (half the distance between point 1 and point 2 - 20pt). And then do the same with the other half of the line (so that you now have a space in the middle of the line.
You can take advantage of the Core Graphics Framework to draw shapes in iOS. This allows you to draw lines, circles, arrows, rectangles etc. Here is some example code to draw a line between 2 points (taken from: How do I draw a line on the iPhone?):
CGContextRef c = UIGraphicsGetCurrentContext();
CGFloat red[4] = {1.0f, 0.0f, 0.0f, 1.0f};
CGContextSetStrokeColor(c, red);
CGContextBeginPath(c);
CGContextMoveToPoint(c, 5.0f, 5.0f);
CGContextAddLineToPoint(c, 50.0f, 50.0f);
CGContextStrokePath(c);
First define a subclass of UIView to create a space to draw in. Then use a UITapGestureRecognizer to detect taps. As #WyattMufson suggested in his answer, I would get the user to tap once to get the start point of the line and then tap again to get the end point. This is done to ensure that only straight lines can be drawn.
Once you have the start point and end point you can calculate the mid point and connect the two points with a '?' character in between. Then save these line coordinates (start, mid and end points) in some sort of data structure to keep track of all the lines that have been drawn.
When the user taps on a specific line, you can use the saved line coordinates information to detect whether the tap occurred on a line or not (you will have to perform some calculations to do this). If so, display a popover that accepts user input. Once the user inputs a value, close the popover and replace the '?' with the new value.
For line deletion, you could use the UILongPressGestureRecognizer. The user would tap and hold on a drawn line, which would bring up a popover to confirm whether the user wants to proceed with deletion or not. If they do, access the saved line coordinates to detect whether the hold occurred on a line or not. If so, erase that line.
Here are some references to get you started:
How make a simple drawing app with UIKit
How do I draw a line on the iPhone?
Check is a point (x,y) is between two points drawn on a straight line
How to get UITouch location from UIGestureRecognizer