Learn Pain Less

HomeOur TeamContact
Python
How to Rotate and Scale a Vector in Python
Pawneshwer Gupta
Pawneshwer Gupta
March 06, 2023
1 min

Table Of Contents

01
Rotation of a Vector in Python
02
Scaling of a Vector in Python
03
Combining Rotation and Scaling
04
Conclusion
How to Rotate and Scale a Vector in Python

Rotating and scaling vectors are common operations in computer graphics and machine learning applications. Python provides a rich library of tools for performing such operations. In this article, we will explore how to rotate and scale a vector in Python.

Rotation of a Vector in Python

A vector in two-dimensional space can be represented by a tuple or list of two numbers. The first number represents the horizontal component, and the second represents the vector’s vertical component. We can rotate a vector by a certain angle using the following formula:

import math
def rotate_vector(vector, angle):
x = vector[0] * math.cos(angle) - vector[1] * math.sin(angle)
y = vector[0] * math.sin(angle) + vector[1] * math.cos(angle)
return [x, y]

Here, vector is the vector to be rotated, and angle is the angle (in radians) we want to rotate the vector. The math.cos and math.sin functions are used to compute the cosine and sine of the angle, respectively. The rotated vector is returned as a list of two numbers.

Scaling of a Vector in Python

We can scale a vector by multiplying its horizontal and vertical components by a scaling factor. This can be achieved using the following code:

def scale_vector(vector, scale):
x = vector[0] * scale
y = vector[1] * scale
return [x, y]

Here, vector is the vector to be scaled, and scale is the scaling factor. The scaled vector is returned as a list of two numbers.

Combining Rotation and Scaling

We can combine rotation and scaling operations to achieve more complex transformations. For example, suppose we want to rotate a vector by an angle of 45 degrees and then scale it by a factor of 2. We can achieve this using the following code:

vector = [1, 1]
angle = math.radians(45)
scale = 2
# Rotate the vector
vector = rotate_vector(vector, angle)
# Scale the vector
vector = scale_vector(vector, scale)
print(vector) # Output: [1.414213562373095, 1.414213562373095]

Here, we first define the vector to be transformed (vector), the angle by which we want to rotate the vector (angle), and the scaling factor (scale). We then rotate the vector using the rotate_vector function, and scale the resulting vector using the scale_vector function. The final transformed vector is printed to the console.

Conclusion

In this article, we have explored how to rotate and scale a vector in Python. These operations are useful in various applications, including computer graphics, machine learning, and physics simulations. Using the code snippets provided in this article, you can easily incorporate these operations into your Python projects.

Subscribe to our newsletter!

We'll send you the best of our blog just once a month. We promise.

Tags

Vector

Share


Pawneshwer Gupta

Pawneshwer Gupta

Software Developer

Pawneshwer Gupta works as a software engineer who is enthusiastic in creating efficient and innovative software solutions.

Expertise

Python
Flutter
Laravel
NodeJS

Social Media

Related Posts

Understanding Python deque (Double-Ended Queue)
Understanding Python deque (Double-Ended Queue)
August 23, 2023
2 min
Learn Pain Less  © 2024, All Rights Reserved.
Crafted with by Prolong Services

Quick Links

Advertise with usAbout UsContact Us

Social Media