Drawing a line given the angle and a point on the line - python

In my image I have a triangle (representing an arrow). This arrow defines the direction and area under consideration for further search in same image. For example if I have a triangle rotated at 30 degree w.r.t x-axis and it's tip is located at (250,150) in the image. I would want to find and draw a line normal the tip of triangle, as shown in the image below.
In the image above, I have the angle of blue line in the triangle, to which the normal is to be drawn. Also the tip of the triangle is known which is the only point known on the normal line.
My code for the python function is given below. This code draws a line, passing through the tip but not necessarily NORMAL to the blue line.
def draw_intercepts(img,triangle):
tip=triangle["tip"]
x1=tip[0]
y1=tip[1]
arrow_angle=triangle["arrow_angle"]
y_intercept=int(y1+((1/np.tan(arrow_angle))*x1))
x_intercept=int(x1+(np.tan(arrow_angle)*y1))
cv2.line(img,(x_intercept,0),(0,y_intercept),[0,0,255],3,cv2.LINE_AA)
This code is written by following an answer to this post:
https://math.stackexchange.com/questions/2381119/how-to-find-slope-x-and-y-intercepts-given-angle-to-the-normal-vector-and-a-poi
Note:
I updated the code as recommended:
def draw_intercepts(img,triangle):
tip=triangle["tip"]
x1=tip[0]
y1=tip[1]
arrow_angle=triangle["arrow_angle"]
arrow_angle_rad=np.radians(arrow_angle)
y_intercept=int(y1+((1/np.tan(arrow_angle_rad))*x1))
x_intercept=int(x1+(np.tan(arrow_angle_rad)*y1))
cv2.line(img, (x_intercept, 0), (0, y_intercept), [0, 0, 255], 3, cv2.LINE_AA)
This resolved the issue.
Now the line is drawn perfectly when the arrow_angle is in 1st or 3rd quadrant e.g. 0 < arrow_angle < 90 and 180 < arrow_angle < 270 but in 2nd and 4th quadrant ( 90 < arrow_angle < 180 and 270 < arrow_angle < 360) line angle is not drawn as correct angle or position. Even I don't know it line is drawn somewhere because it is not visible in image.

Note that used line equation "in intercept segments" is not universal - does not work for horizontal and vertical lines. With intercept approach cases 0, Pi/2, Pi, 3*Pi/2 or -Pi/2 must be treated separately.
If you arrow_angle is for blue line:
c = -Sin(arrow_angle)
s = Cos(arrow_angle)
If you arrow_angle is for red line:
c = Cos(arrow_angle)
s = Sin(arrow_angle)
Then draw line through points
(x1 - c * 4096, y1 - s * 4096) and (x1 + c * 4096, y1 + s * 4096)
(I used arbitrary large constant comparable with screen size)

Related

Get points of any regular polygon that can be also rotated

I'm trying to draw any regular polygon, so from triangles to polygons with so many corners, it looks like a circle. to make it easier, they must be regular, so a normal pentagon/hexagon/octagon etc. I want to be able to rotate them. What ive tried is to draw a circle and divide 360 by the amount of points i want then, create a point every nth degrees around the circle, putting these points in pygame.draw.polygon() then creates the shape i want, the problem is it isn't the right size, i also want to be able to have stretch the shapes, so have a different width and height.
def regular_polygon(hapi, x, y, w, h, n, rotation, angle_offset = 0):
#angle_offset is the starting angle in the circle where rotation is rotating the circle
#so when its an oval, rotation rotates the oval and angle_offset is where on the oval to start from
if n < 3:
n = 3
midpoint = pygame.Vector2(x + w//2, y + h//2)
r = sqrt(w**2 + h**2)
#if angle_offset != 0:
#w = (w//2)//cos(angle_offset)
#if angle_offset != 90:
#h = (h//2)//sin(angle_offset)
w,h = r,r
points = []
for angle in range(0, 360, 360//n):
angle = radians(angle + angle_offset)
d = pygame.Vector2(-sin(angle)*w//2, -cos(angle)*h//2).rotate(rotation) #the negative sign is because it was drawing upside down
points.append(midpoint + d)
#draws the circle for debugging
for angle in range(0, 360, 1):
angle = radians(angle + angle_offset)
d = pygame.Vector2(-sin(angle)*w//2, -cos(angle)*h//2).rotate(rotation)
pygame.draw.rect(screen, (0,255,0), (midpoint[0] + d[0], midpoint[1] + d[1], 5, 5))
pygame.draw.polygon(screen,(255,0,0),points)
the red square is the the function above is making, the blue one behind is what it should be
as you can see the circle does line up with the edges of the rect, but because the angles of the circle are not even, the rectangle the func makes is not right.
i think i need to change the circle to an oval but cannot find out how to find the width radius and height radius of it. currently ive found the radius by using pythag.
this is what happens when i dont change the width or height
I found the solution
doing w *= math.sqrt(2) and h *= math.sqrt(2) works perfectly. dont quite understand the math, but after trial and error, this works. You can probable find the maths here but i just multiplied the width and height by a number and printed that number when it lined up which was very close to the sqrt(2)

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).

How to increase elliptical arc resolution for high radius values in opencv?

I've been trying to draw an elliptical arc in openCV using the ellipse function (https://docs.opencv.org/3.0-beta/modules/imgproc/doc/drawing_functions.html), however, for high radius values the arcs seem segmented.
Do you know how can I sort of increase the arc's resolution to appear better for high radius values?
I tried to draw an arc with a small radius and it looked smooth and I also tried increasing image resolution but no difference was noticed.
My code is as follows:
A[0] = round(A[0]*dpm - Xmin + margin) #Normalize CenterX
A[1] = round(A[1]*dpm - Ymin + margin) #Normalize CenterY
A[2] = round(A[2]*dpm) #Normalize Radius
startAng = A[3]
endAng = A[4]
A=A.astype(int)
cv2.ellipse(Blank,(A[0],A[1]),(A[2],A[2]), 0, startAng, endAng, 0 ,1)
while:
Blank is the image I want to draw the arc on (np array, size= (398, 847)
(A[0],A[1]) is the center point
(A[2],A[2]) ellipse axes
0 is the angle
startAng is the starting angle of the arc
endAng is the ending angle of the arc
0 is the line color (Black)
1 is the line thickess
The code should produce a smooth arc but it looks segmented as if it is made of 4 lines.
I ended up writing a function to plot an arc on an input image:
import numpy as np
import cv2
blank = np.ones((500,500))
def DrawArc(image, center, radius, startAng, endAng, color,resolution):
'''Draws an arc with specific reslution and color on an input image
Args:
image - The input image to draw the arc on
center - Arc's center
radius - Arc's radius
startAng - the starting angle of the arc
engAng - the ending angle of the arc
color - Arc's color on the input image
resolution - Number of points for calculation
output:
image - updated image with plotted arc'''
startAng += 90
endAng += 90
theta = np.linspace(startAng,endAng,resolution)
x = np.round(radius*np.cos(np.deg2rad(theta))) + center[0]
y = np.round(radius*np.sin(np.deg2rad(theta))) + center[1]
x=x.astype(int)
y=y.astype(int)
for k in range(np.size(theta)):
image[x[k]][y[k]] = color
return image
image = DrawArc(blank,(250,250),200,0,90,0,1000)
cv2.imshow("Arc",image)
cv2.waitKey()
The output image is
Output

Efficient way to find the pixel coordinates in image for circle circumference intersection

I would like to efficiently find the coordinates of the line described by the intersection between the circumference of a circle and an image (origin of circle is outside the image). Right now I'm using a loop in python to start at one edge of the image and move through the image a step at a time. Each step moves a certain distance (say 0.01 inches). I calculate the angle needed to move that distance and then use polar geometry formulas to define the next pixel coordinate. This all works just fine, however, it takes a long time. I'm creating many of these lines through the image as the radius of the circle increases.
Is there a way to use a built in function or an array based formula so that I don't have to have so many steps in my algorithm? Basically, what is the most efficient way to accomplish this in python 2?
Thanks,
rb3
# circle parameters
x0 = -5
y0 = -5
R = 25
# image size
max_x = 100
max_y = 100
# sample points
theta = np.linspace(0, 2*np.pi, 2048) # make bigger if you have huge images
# the pixels that get hit
xy = list(set([xy for xy in zip( (R * cos(theta) - x0).astype(int), (R * sin(theta) - y0).astype(int)) if
xy[0] >= 0 and xy[0] < max_x and xy[1] >= 0 and xy[1] < max_y]))

Koch snowflake implementation in python using pygame

I would like to implement Koch Koch snow flake using pygame.
I am working with the following series of images from http://en.wikipedia.org/wiki/File:KochFlake.svg
My algorithm is this
Draw a triangle
Calculate the points of a triangle one-third its size and delete the centre line
Find out the outer points (as show in the second figure of the above image)
Make a list of all end points
Using polygon join all points
I've done up to the second step. But I'm struggling on the third step - as I can't work out how to find the outer points - any tips?
Here is my code up to second step
import pygame
from pygame.locals import *
pygame.init()
fpsClock = pygame.time.Clock()
screen = pygame.display.set_mode((600,600))
pygame.display.set_caption('Koch snowflake')
white = (255, 255, 255)
black = (0, 0 ,0)
def midpoints(pt1 , pt2):
(x1, y1) = pt1
(x2, y2) = pt2
return ((x1+x2)/2, (y1 + y2)/2)
def midline(pt1, pt2):
(x1, y1) = pt1
(x2, y2) = pt2
return [(x1 + float(x2-x1)/3.0,y1 + float(y2-y1)/3.0), (x1 + float(x2-x1)*2.0/3,y1+ float(y2-y1)*2.0/3)]
def drawline(pt1, pt2):
pygame.draw.line(screen, white, pt1, pt2)
def clearline(pt1,pt2):
pygame.draw.line(screen, black, pt1, pt2, 4)
a = [(150,150), (450,150), (300,410), (150,150)]
pygame.draw.polygon(screen, white ,(a[0], a[1], a[2]), 1)
i = 0
order = 0
length = len(a)
while order < length - 1:
pts = midline(a[i], a[i+1])
clearline(pts[0], pts[1])
a = a[:i+1] + pts + a[i+1:]
print a
if order < 3:
i = i+3
order = order + 1
#pygame.draw.polygon(screen, white ,Tup, 1)
pygame.display.update()
Not exactly an answer but something relevant to your larger question.
L-system fractals (like what you're trying to draw here) are best done using a rudimentary L-system parser. For a Koch snowflake, the 'axiom' (which is a description of the initial shape is something like this) D++D++D++. The D stands for "move forward by one unit" and the + for "turn clockwise by 30 degrees". The instructions will be "interpreted" by a turtle like cursor. It's not very hard to do this.
Once the axiom is drawn, you have a segment that replaces the D. For the koch flake, it is D-D++D-D meaning "move forward one unit, turn anticlockwise 30 degrees, forward, clockwise 60, forward, anticlockwise 30 and forward". This gives you the _/\_ shape that replaces the sides of the initial triangle. One "unit" reduces by to one third of the original length on every iteration.
Now, repeat this as many times as you want and you're looking for. This was one of my earliest Python programs and I have a crude parser/interpreter for it on github. It doesn't use pygame but you should be able to swap that part out quite easily.
To calculate the points, I'd use a vector approach. If the triangle's corners are a1, a2 and a3, then you can get an equation for all points on the line a1 to a2. Using that equation, you can find the points at 1/3 and 2/3 between a1 and a2. The distance between those points gives you the side of the new triangle you're going to create. Using that information, and the point at 1/2 between a1 and a2 you can work out the coordinates of the third new point.

Categories

Resources