Find points in an elliptical orbit

# Points in an ellipse

import pygame
import math

pygame.init()
size = width, height = 600, 300
screen = pygame.display.set_mode((width, height))

xRadius = 250
yRadius = 100

for degree in range(360,0,-15):
    x = int((math.cos(degree * 2 * math.pi / 360) * xRadius) + width/2)
    y = int((math.sin(degree * 2 * math.pi / 360) * yRadius) + height/2)

    #  Prints the  x and y coordinates of points in ellipse
    print(x,y)
   
    pygame.draw.circle(screen, (0, 125, 255), [x, y], 15)
    pygame.display.flip()
   
OUTPUT:



550 149
541 124
516 99
476 79
425 63
364 53
299 50
235 53
174 63
123 79
83 99
58 124
50 150
58 175
83 200
123 220
175 236
235 246
300 250
364 246
425 236
476 220
516 200
541 175
 




Sum of an array of numbers

# Sum of an array of numbers

no_of_terms = int(input("Enter number of elements in array : "))
list1 = []

for val in range(0,no_of_terms,1):
    ele = int(input("Enter integer : "))
    list1.append(ele)

print("Sum of elements of list ", sum(list1))


Distance between two points

# Distance between two points

import math

print("Enter coordinates for Point 1 : ")
x1 = int(input("x1 = "))
y1 = int(input("y1 = "))


print("Enter coordinates for point 2 : ")
x2 = int(input("x2 = "))
y2 = int(input("y2 = "))


dist = math.sqrt( ((x2-x1)**2) + ((y2-y1)**2) )

print("Distance between given points is", round(dist,2))

Circulate the values of n variables

# Circulate the values of n variables


no_of_terms = int(input("Enter number of values : "))

#Read values
list1 = []
for val in range(0,no_of_terms,1):
    ele = int(input("Enter integer : "))
    list1.append(ele)


#Circulate and display values
print("Circulating the elements of list ", list1)
   
for val in range(0,no_of_terms,1):
    ele = list1.pop(0)
    list1.append(ele)
    print(list1)

Swap the values of two variables

# Swap the values of two variables

num1 = int(input("Enter a number : "))
num2 = int(input("Enter a number : "))

print("Values of variables before swap :")
print("Number 1 :",num1)
print("Number 2 :",num2)

num1, num2 = num2, num1

print("Values of variables before swap :")
print("Number 1 :",num1)
print("Number 2 :",num2)

Binary search

# Binary search

list1 = [11,22,13,44,65,56,87,88,79,10]

list1.sort()
num = int(input("Enter element to search : "))

bottom = 0
top = len(list1)-1



while(True):
    if (bottom > top):
        print("Element not found")
        break
   
    middle = (bottom+top)//2
    if(list1[middle] == num):
        print("Sorted list :", list1)
        print("Element", num, "present at index", middle)
        break
    elif (list1[middle] < num):
        bottom = middle + 1
    else:
        top = middle - 1

Linear Search

# Linear Search
import sys

list1 = [11,22,13,44,65,56,87,88,79,10]

num = int(input("Enter element to search : "))

for ind in range(0, len(list1),1):
    if (list1[ind] == num):
        print("Element", num, "present at index", ind)
        sys.exit(0)

print("Element not present in list")

First n prime numbers

# First n prime numbers

print("\n\nFirst n prime numbers")
no_of_terms = int(input("Enter number of terms : "))

if (no_of_terms > 0):
    print("The prime numbers are : ")
    num = 1
    counter = 0
    while(counter < no_of_terms):
        flag = True
        if (num >= 1 and num <= 3):
            pass
        elif ((num % 2)==0):
            flag = False
        else:
            for div in range(3, num//2, 2):
                if ((num % div) == 0):
                    flag = False
                    break
           
        if (flag == True):
            print(num)
            counter += 1
                       
        num +=1
else:
    print("Invalid number of terms")

Find maximum in a list of numbers - Lists

# Find maximum in a list of numbers

print("\n\nFind maximum in a list of numbers")

no_of_terms = int(input("Enter number of terms : "))
list1 = []

for counter in range(0,no_of_terms,1):
    num = int(input("Enter number : "))
    list1.append(num)
   
print("Given list of numbers :", list1)
print("Maximum value in the list of numbers is", max(list1))

Find maximum in a list of numbers - Control structures

# Find maximum in a list of numbers

print("\n\nFind maximum in a list of numbers")

no_of_terms = int(input("Enter number of terms : "))
max_num = 0

for counter in range(0,no_of_terms,1):
    num = int(input("Enter number : "))
    if (num > max_num):
        max_num = num
       
print("Maximum value in the list of numbers is", max_num)

Exponentiation (Power of a number)

# Exponentiation (Power of a number)

print("\n\nExponentiation (Power of a number)")
base = int(input("Enter base : "))
power = int(input("Enter power : "))

print(base, "to the power of", power , "is", (base ** power))

Square root of a number - Newtons method

# Square root of a number - Newtons method

print("\n\nSquare root of a number - Newtons method")
num1 = int(input("Enter a number : "))
x = num1//2

while (True):
    y = (x + num1/x) / 2
    if (y == x):
        print("Square root of", num1, "is", round(y,4))
        break
    x = y

GCD of two numbers

# GCD of two numbers

num1 = int(input("Enter a number : "))
num2 = int(input("Enter a number : "))

if (num1 == 0 or num2 == 0):
    print("GCD of", num1,"and", num2 , "is 0")
else:
    tmp1, tmp2 = num1, num2

    while(tmp1 != tmp2):
        if (tmp1 > tmp2):
            tmp1 = tmp1 - tmp2
        else:
            tmp2 = tmp2 - tmp1
           
    print("GCD of", num1, "and", num2 , "is", tmp1)
   

Simulate Elliptical Orbit in Pygame

#Elliptical orbits

import pygame
import math
import sys

pygame.init()

screen = pygame.display.set_mode((600, 300))
pygame.display.set_caption("Elliptical orbit")

clock = pygame.time.Clock()

while(True):
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
   
    xRadius = 250
    yRadius = 100
   
    for degree in range(0,360,10):
        x1 = int(math.cos(degree * 2 * math.pi / 360) * xRadius) + 300
        y1 = int(math.sin(degree * 2 * math.pi / 360) * yRadius) + 150
        screen.fill((0, 0, 0))
        pygame.draw.circle(screen, (255, 0, 0), [300, 150], 35)
        pygame.draw.ellipse(screen, (255, 255, 255), [50, 50, 500, 200], 1)
        pygame.draw.circle(screen, (0, 0, 255), [x1, y1], 15)
       
        pygame.display.flip()
        clock.tick(5)


Algorithmhttps://drranurekha.blogspot.com/2019/10/algorithm-simulation-of-elliptical-orbit_58.html



Output:








Bouncing ball, python

# Bouncing ball


# Program
# Bouncing ball

import sys, pygame
pygame.init()


size = width, height = 700, 250
speed = [1, 1]
background = 255, 255, 255
screen = pygame.display.set_mode(size)


pygame.display.set_caption("Bouncing ball")


ball = pygame.image.load("ball.jpg")
ballrect = ball.get_rect()


while 1:
pygame.time.delay(2)

for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit()

ballrect = ballrect.move(speed)
if ballrect.left < 0 or ballrect.right > width:
speed[0] = -speed[0]
if ballrect.top < 0 or ballrect.bottom > height:
speed[1] = -speed[1]

screen.fill(background)
screen.blit(ball, ballrect)
pygame.display.flip()


   


ball.jpg
   

Output: