Calculate angles that are in degrees (clockwise) to radians (counter clockwise) - python

First, I am aware of how converting degrees in radians.
I need to draw an arc given a coordinate, radius, and two angles in degrees clockwise in pygame. For example:
point = (50, 50)
startAngle = 320
endAngle = 140
should draw something like
and
point = (50, 50)
startAngle = 90
endAngle = 180
should draw something like
etc.
I have tried reverting the angle (i.e. 360 - angle) but pygame draws the arc in reverse; instead of a 45⁹ arc in the last image, I get a 270⁹ arc that is the complement of what I want.
I guess I'm having a brain fart day because I can't figure this out. Thank you!
Edit : I may have an answer, though I'm not sure if it is a good one. If I reverse the angles and negate them, it seems to draw them correctly clockwise. For example, given the first example :
point = (50, 50)
startAngle = 360 - 140
endAngle = 360 - 320
seems to correctly draws the arc where expected.

If you want to draw an clockwise arc, then you've to invert the angles and you've to swap the start and end angle:
def clockwiseArc(surface, color, point, radius, startAngle, endAngle):
rect = pygame.Rect(0, 0, radius*2, radius*2)
rect.center = point
endRad = math.radians(-startAngle)
startRad = math.radians(-endAngle)
pygame.draw.arc(surface, color, rect, startRad, endRad)
e.g.:
clockwiseArc(window, (255, 0, 0), (150, 70), 50, 300, 140)
clockwiseArc(window, (255, 0, 0), (300, 70), 50, 90, 180)

Related

black area between overlapped circles in opencv - python

I am implementing "Bubbles algorithm" for classification recognition analysis.
in this algorithm, we have to make mask with gaussian circles.
the center of circles would be 1(255) and it will decrease over the radius which will be 0.
i have problem when i put circles on each other for creating the mask, it will put a black line between circles and i can not remove it.
this is my code:
def make_gaussian(circle_center, Gaussian_base):
for t in range(circle_center[1] - radius, circle_center[1] + radius):
for tt in range(circle_center[0] - radius, circle_center[0] + radius):
distance = int(compute_distance(tt, circle_center[0], t, circle_center[1]))
if distance < radius :
value = int(((radius - distance) * (1 / radius)) * 255)
if Gaussian_base[t][tt].mean() < value:
Gaussian_base[t][tt] = [value, value, value]
return Gaussian_base
Gaussian_base = np.zeros((height, width, 3), np.uint8)
for s in centers:
#xmin, ymin, xmax, ymax
Gaussian_base = make_gaussian(s, Gaussian_base)
cv2.imwrite('gaussianMask.jpg', Gaussian_base)
you can use this list as centers:
centers = [(139, 102), (223, 193), (94, 385), (205, 301), (90, 147), (190, 209), (45, 349), (193, 259), (110, 343), (159, 99)]
and the output is like this:
enter image description here
the problem is the black line(area) between two overlapped circles which should be removed and the joint area should be continous.
Try this...
def make_gaussian(circle_center, Gaussian_base):
for t in range(circle_center[1] - radius, circle_center[1] + radius):
for tt in range(circle_center[0] - radius, circle_center[0] + radius):
distance = int(compute_distance(tt, circle_center[0], t, circle_center[1]))
if distance < radius :
value = int(((radius - distance) * (1 / radius)) * 200)
#if Gaussian_base[t][tt].mean() < value:
Gaussian_base[t][tt] += np.array([value, value, value], dtype=np.uint8)
return Gaussian_base
the "black line" in your posted image between balls is an optical illusion. it is a local minimum and your eyes recognize that.
open your image in an image editor. take the color picker. move along a ball and into the "black line". the indicated color/brightness moves smoothly. it doesn't dip and it's not black.

Pygame: How do I blit and rotate an image to connect two points on the screen?

Here is a test program. I started with two random dots and the line connecting them. Now, I want to take a given image (with x,y dimensions of 79 x 1080) and blit it on top of the guide line. I understand that arctan will give me the angle between the points on a cartesian grid, but because y is backwards the screen (x,y), I have to invert some values. I'm confused about the negating step.
If you run this repeatedly, you'll see the image is always parallel to the line, and sometimes on top, but not consistently.
import math
import pygame
import random
pygame.init()
screen = pygame.display.set_mode((600,600))
#target = (126, 270)
#start = (234, 54)
target = (random.randrange(600), random.randrange(600))
start = (random.randrange(600), random.randrange(600))
BLACK = (0,0,0)
BLUE = (0,0,128)
GREEN = (0,128,0)
pygame.draw.circle(screen, GREEN, start, 15)
pygame.draw.circle(screen, BLUE, target, 15)
pygame.draw.line(screen, BLUE, start, target, 5)
route = pygame.Surface((79,1080))
route.set_colorkey(BLACK)
BMP = pygame.image.load('art/trade_route00.png').convert()
(bx, by, bwidth, bheight) = route.get_rect()
route.blit(BMP, (0,0), area=route.get_rect())
# get distance within screen in pixels
dist = math.sqrt((start[0] - target[0])**2 + (start[1] - target[1])**2)
# scale to fit: use distance between points, and make width extra skinny.
route = pygame.transform.scale(route, (int(bwidth * dist/bwidth * 0.05), int( bheight * dist/bheight)))
# and rotate... (invert, as negative is for clockwise)
angle = math.degrees(math.atan2(-1*(target[1]-start[1]), target[0]-start[0]))
route = pygame.transform.rotate(route, angle + 90 )
position = route.get_rect()
HERE = (abs(target[0] - position[2]), target[1]) # - position[3]/2)
print(HERE)
screen.blit(route, HERE)
pygame.display.update()
print(start, target, dist, angle, position)
The main problem
The error is not due to the inverse y coordinates (0 at top, max at bottom) while rotating as you seems to think. That part is correct. The error is here:
HERE = (abs(target[0] - position[2]), target[1]) # - position[3]/2)
HERE must be the coordinates of the top-left corner of the rectangle inscribing your green and blue dots connected by the blue line. At those coordinates, you need to place the Surface route after rescaling.
You can get this vertex by doing:
HERE = (min(start[0], target[0]), min(start[1], target[1]))
This should solve the problem, and your colored dots should lay on the blue line.
A side note
Another thing you might wish to fix is the scaling parameter of route:
route = pygame.transform.scale(route, (int(bwidth * dist/bwidth * 0.05), int( bheight * dist/bheight)))
If my guess is correct and you want to preserve the original widht/height ratio in the rescaled route (since your original image is not a square) this should be:
route = pygame.transform.scale(route, (int(dist* bwidth/bheight), int(dist)))
assuming that you want height (the greater size in the original) be scaled to dist. So you may not need the 0.05, or maybe you can use a different shrinking parameter (probably 0.05 will shrink it too much).

Anti-aliased Arc Pygame

I am trying to code a simple circle timer in Python using Pygame.
At the moment it looks like this:
As you can see, the blue line is very wavy and has white dots in it. I am achieving this blue line by using pygame.draw.arc() function, but it is not anti-aliased and looks bad. I would like it to be anti-aliased, but gfxdraw module which should let me achieve this, doesn't support arc width selection. Here's code snippet:
pygame.draw.arc(screen, blue, [center[0] - 120, center[1] - 120, 240, 240], pi/2, pi/2+pi*i*koef, 15)
pygame.gfxdraw.aacircle(screen, center[0], center[1], 105, black)
pygame.gfxdraw.aacircle(screen, center[0], center[1], 120, black)
I did it creating the arc with a polygon.
def drawArc(surface, x, y, r, th, start, stop, color):
points_outer = []
points_inner = []
n = round(r*abs(stop-start)/20)
if n<2:
n = 2
for i in range(n):
delta = i/(n-1)
phi0 = start + (stop-start)*delta
x0 = round(x+r*math.cos(phi0))
y0 = round(y+r*math.sin(phi0))
points_outer.append([x0,y0])
phi1 = stop + (start-stop)*delta
x1 = round(x+(r-th)*math.cos(phi1))
y1 = round(y+(r-th)*math.sin(phi1))
points_inner.append([x1,y1])
points = points_outer + points_inner
pygame.gfxdraw.aapolygon(surface, points, color)
pygame.gfxdraw.filled_polygon(surface, points, color)
The for loop could certainly be created more elegantly with a generator, but I am not very sophisticated with python.
The arc definitely looks nicer than pygame.draw.arc, but when I compare it to the screen rendering on my mac, there is room for improvement.
I am not aware of any pygame function that would solve this problem, meaning you basically have to program a solution yourself (or use something other than pygame), since draw is broken as you've noted and gfxdraw won't give you the thickness.
One very ugly but simple solution is to draw multiple times over the arc segments, always slightly shifted to "fill in" the missing gaps. This will still leave some aliasing at the very front of the timer arc, but the rest will be filled in.
import pygame
from pygame.locals import *
import pygame.gfxdraw
import math
# Screen size
SCREEN_HEIGHT = 350
SCREEN_WIDTH = 500
# Colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREY = (150, 150, 150)
RED = (255,0,0)
# initialisation
pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
done = False
clock = pygame.time.Clock()
# We need this if we want to be able to specify our
# arc in degrees instead of radians
def degreesToRadians(deg):
return deg/180.0 * math.pi
# Draw an arc that is a portion of a circle.
# We pass in screen and color,
# followed by a tuple (x,y) that is the center of the circle, and the radius.
# Next comes the start and ending angle on the "unit circle" (0 to 360)
# of the circle we want to draw, and finally the thickness in pixels
def drawCircleArc(screen,color,center,radius,startDeg,endDeg,thickness):
(x,y) = center
rect = (x-radius,y-radius,radius*2,radius*2)
startRad = degreesToRadians(startDeg)
endRad = degreesToRadians(endDeg)
pygame.draw.arc(screen,color,rect,startRad,endRad,thickness)
# fill screen with background
screen.fill(WHITE)
center = [150, 200]
pygame.gfxdraw.aacircle(screen, center[0], center[1], 105, BLACK)
pygame.gfxdraw.aacircle(screen, center[0], center[1], 120, BLACK)
pygame.display.update()
step = 10
maxdeg = 0
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
maxdeg = maxdeg + step
for i in range(min(0,maxdeg-30),maxdeg):
drawCircleArc(screen,RED,(150,200),119,i+90,max(i+10,maxdeg)+90,14)
#+90 will shift it from starting at the right to starting (roughly) at the top
pygame.display.flip()
clock.tick(2) # ensures a maximum of 60 frames per second
pygame.quit()
Note that I have copied degreesToRadians and drawCircleArc from https://www.cs.ucsb.edu/~pconrad/cs5nm/08F/ex/ex09/drawCircleArcExample.py
I do not generally recommend this solution, but it might do in a pinch.
You are right, some pygame rendering functions do indeed suck, so you can achieve something like this with PIL instead.
pie_size = (40, 40) # defining constants
pil_img = PIL.Image.new("RGBA", pie_size) # PIL template image
pil_draw = PIL.ImageDraw.Draw(pil_img) # drawable image
pil_draw.pieslice((0, 0, *[ps - 1 for ps in pie_size]), -90, 180, fill=(0, 0, 0)) # args: (x0, y0, x1, y1), start, end, fill
This will create a PIL shape. Now we can convert it to pygame.
data = pil_img.tobytes()
size = pil_img.size
mode = pil_img.mode
pygame_img = pygame.image.fromstring(data, size, mode).convert_alpha()
But don't forget to pip install pillow and
import PIL.Image
import PIL.ImageDraw
Ok, this is really old, but why not try to draw pies instead. For example draw a pie, then an unfilled circle as the outside ring and then a filled circle as the inside and another unfilled circle as the inside ring.
So pie -> unfilled circle -> filled circle -> unfilled.
The order is somewhat arbitrary but if u still have this problem give it a try. (Btw I haven't tried it but I think it will work)
For my own uses, I wrote a simple wrapper function, and to deal with the spotty arc drawing, I used an ugly loop to draw the same arc several times.
def DrawArc(surface, color, center, radius, startAngle, stopAngle, width=1):
width -= 2
for i in range(-2, 3):
# (2pi rad) / (360 deg)
deg2Rad = 0.01745329251
rect = pygame.Rect(
center[0] - radius + i,
center[1] - radius,
radius * 2,
radius * 2
)
pygame.draw.arc(
surface,
color,
rect,
startAngle * deg2Rad,
stopAngle * deg2Rad,
width
)
I'm aware this is not a great solution, but it works alright for my uses.
An important note is I added that "width -= 2" to hopefully preserve the intended size of the arc at least a little more accurately, but this results in increasing the minimum width by 2.
In your case, you might want to consider doing something more to fix the issues this results in.
If the start and end aren't all that important, one can create many circles following an arc trajectory and when done ie small circles drawn 360 time, you finally have a big circle with no wavy effect:
MWE:
#!/usr/bin/env python3
import pygame
import math
# Initialize pygame
pygame.init()
# Set the screen size
screen = pygame.display.set_mode((400, 300))
# Set the center point of the arc
center_x = 200
center_y = 150
arc_radius = 100
circle_radius = 6
# Set the start and stop angles of the arc
start_angle = 0
stop_angle = 360
angle_step = 1
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Clear the screen
screen.fill((0, 0, 0))
# Draw the overlapping circles
for i in range(start_angle, stop_angle, angle_step):
angle = math.radians(i)
x = center_x + arc_radius * math.cos(angle)
y = center_y + arc_radius * math.sin(angle)
pygame.draw.circle(screen, "red", (int(x), int(y)), circle_radius)
# Update the display
pygame.display.flip()
pygame.quit()
Having a start_angle and stop_angle of 0 to 360 respectively yields a fill circle with an output:
To change it to a 1/3 of a circle, one would change the stop_angle from 360 to 120 (1/3 x 360 = 120) and this would then yield:

python draw pie shapes with colour filled

I am trying to draw a pie shape with filled colour. I've tried to do this in different ways. Here's the code:
ball = pygame.draw.circle(self.screen, self.pink, self.pos, self.r, 0)
pygame.gfxdraw.pie(self.screen, 60,60, 40, 0, 90,(0,255,0))
pygame.gfxdraw.arc(self.screen, 60,60, 40, 180, 270,(0,255,255))
pygame.draw.arc(self.screen, (255,0,255),ball,0, math.pi/4, ball.width/2)
The output image is like:
I want the pie shapes filled with colour, as the magenta coloured shape does. I used the arc function and set the line with = the radius to achieve this (4th line in the code). However, the colour isn't evenly filled. I also tried to draw a pie shape (2nd line in the code) However, I cannot find a way to fill the colour...
Thank you very much for your help!
You can just draw a sufficiently fine polygon (e.g in one degree intervals):
import math
import pygame
# Center and radius of pie chart
cx, cy, r = 100, 320, 75
# Background circle
pygame.draw.circle(screen, (17, 153, 255), (cx, cy), r)
# Calculate the angle in degrees
angle = val*360/total
# Start list of polygon points
p = [(cx, cy)]
# Get points on arc
for n in range(0,angle):
x = cx + int(r*math.cos(n*math.pi/180))
y = cy+int(r*math.sin(n*math.pi/180))
p.append((x, y))
p.append((cx, cy))
# Draw pie segment
if len(p) > 2:
pygame.draw.polygon(screen, (0, 0, 0), p)

Rotate Cube to 'look at' mouse from Python in OpenGL

I am working on making a proof of concept bubble popper game, so I need the cannon to follow the mouse. I am currently trying to have a cube rotate around the z axis to follow the mouse. I am using the code below and it produces the results below that. The cannon sits in the middle of the bottom of a 550 x 550 window. The results printed below the code are when the mouse is at the lower right corner, the center of the window, and the lower left. So I would anticipate the resulting angles to be -90, ~0, 90 But well, not so much. This may turn out to be a programming issue or it may turn out to be a math issue. I am pretty sure the math works because I tested it outside of the mouse position and it gave me the proper results. Can you see the problem?
I have also tried normalizing the vectors first and swapping which points were put in first, it didn't do anything.
I have also included the code I use to set up the window and drawing space.
def cannon_rotation(self):
vector1 = self.points_to_vector((self.width/2, 20), (self.width/2, 30))
vector2 = self.points_to_vector((self.width/2, 20), self.mouse_location)
print 'vector1', vector1
print 'vector2', vector2
a = self.angle(vector1, vector2)
print a
return a
def points_to_vector(self, point1, point2):
return point2[0] - point1[0], point2[1] - point1[1]
def dot_product(self, vector1, vector2):
return vector1[0] * vector2[0] + vector1[1] * vector2[1]
def length(self, vector):
return (self.dot_product(vector, vector)) ** .5
def angle(self, vector1, vector2):
dot_a_b = self.dot_product(vector1, vector2)
len_a_b = (self.length(vector1)) * (self.length(vector2))
angle = dot_a_b / len_a_b
print 'dot_a_b', dot_a_b
print 'len_a_b', len_a_b
print 'angle', angle
angle_in_degrees = acos(angle) * 180 / pi
print angle_in_degrees
return angle_in_degrees
###Create Window
def reshape(self, height, width):
if height >= 90 and width >= 90:
self.index_location_dict = self.create_index_location_dict(height, width)
self.height = height
self.width = width
glViewport(0, 0, height, width)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glOrtho(0.0, height, width, 0.0, -20.0, 20.0)
glEnable(GL_DEPTH_TEST)
else:
self.game_over = True
The results:
Mouse Position (535, 536)
vector1 (0, 10)
vector2 (260, 516)
dot_a_b 5160
len_a_b 5778.02734504
result 0.893038348881
angle 26.7424369246
Mouse Position (276, 386)
vector1 (0, 10)
vector2 (1, 366)
dot_a_b 3660
len_a_b 3660.01366118
result 0.999996267452
angle 0.15654545612
Mouse Position(9, 535)
vector1 (0, 10)
vector2 (-266, 515)
dot_a_b 5150 len_a_b
5796.38680559
result 0.888484528851
angle 27.316573085
(self.width/2, 20) specifies the center of the top edge. You need to swap it to (self.width/2, height - 20).
You don't need to calculate vector1 this way. It can always be set to (0, 1) (pointing downwards).
Furthermore, check your projection matrix:
glOrtho(0.0, height, width, 0.0, -20.0, 20.0)
Either you have a different OpenGL than me or you have mixed up the parameters:
void glOrtho( GLdouble left,
GLdouble right,
GLdouble bottom,
GLdouble top,
GLdouble nearVal,
GLdouble farVal);

Categories

Resources