Dot Product and Cross Product

The dot product of two vectors is simply the sum of the products of the corresponding elements. If we have two vectors (x1, y1) and (x2, y2), then the dot product of the two vectors is (x1 * x2) + (y1 * y2). The dot product of two vectors is a scalar quantity.

If we have two vectors in x-y plane (x1, y1) and (x2, y2), then the magnitude of cross product of two vectors is (x1 * y2) – (x2 * y1) and direction is in ±z direction. The cross product of two vectors is a vector quantity. Similarly, the cross product of two 3D vectors (x1, y1, z1) and (x2, y2, z2) is (y1*z2 – y2*z1, z1*x2 – z2*x1, x1*y2 – x2*y1).

Implementation:

C++

#include <iostream> #include <vector> using namespace std; // Function to calculate the dot product of two vectors double dotProduct(const vector<double>& vec1, const vector<double>& vec2) { double result = 0; for (int i = 0; i < 3; ++i) { result += vec1[i] * vec2[i]; } return result; } // Function to calculate the cross product of two vectors vector<double> crossProduct(const vector<double>& vec1, const vector<double>& vec2) { vector<double> result(3, 0); result[0] = vec1[1] * vec2[2] - vec1[2] * vec2[1]; result[1] = vec1[2] * vec2[0] - vec1[0] * vec2[2]; result[2] = vec1[0] * vec2[1] - vec1[1] * vec2[0]; return result; } int main() { // Example vectors vector<double> vector1 = { 1.0, 2.0, 3.0 }; vector<double> vector2 = { 4.0, 5.0, 6.0 }; // Calculate and display the dot product double dotResult = dotProduct(vector1, vector2); cout << "Dot Product: " << dotResult << endl; // Calculate and display the cross product vector<double> crossResult = crossProduct(vector1, vector2); cout << "Cross Product: "; for (double value : crossResult) { cout << value << " "; } cout << endl; return 0; }

Java

import java.util.Arrays; import java.util.List; public class VectorOperations { // Function to calculate the dot product of two vectors public static double dotProduct(List<Double> vec1, List<Double> vec2) { double result = 0; for (int i = 0; i < 3; ++i) { result += vec1.get(i) * vec2.get(i); } return result; } // Function to calculate the cross product of two vectors public static List<Double> crossProduct(List<Double> vec1, List<Double> vec2) { List<Double> result = Arrays.asList(0.0, 0.0, 0.0); result.set(0, vec1.get(1) * vec2.get(2) - vec1.get(2) * vec2.get(1)); result.set(1, vec1.get(2) * vec2.get(0) - vec1.get(0) * vec2.get(2)); result.set(2, vec1.get(0) * vec2.get(1) - vec1.get(1) * vec2.get(0)); return result; } public static void main(String[] args) { // Example vectors List<Double> vector1 = Arrays.asList(1.0, 2.0, 3.0); List<Double> vector2 = Arrays.asList(4.0, 5.0, 6.0); // Calculate and display the dot product double dotResult = dotProduct(vector1, vector2); System.out.println("Dot Product: " + dotResult); // Calculate and display the cross product List<Double> crossResult = crossProduct(vector1, vector2); System.out.print("Cross Product: "); for (double value : crossResult) { System.out.print(value + " "); } System.out.println(); } }

Python3

class VectorOperations: @staticmethod def dot_product(vec1, vec2): result = 0 for i in range(3): result += vec1[i] * vec2[i] return result @staticmethod def cross_product(vec1, vec2): result = [0.0, 0.0, 0.0] result[0] = vec1[1] * vec2[2] - vec1[2] * vec2[1] result[1] = vec1[2] * vec2[0] - vec1[0] * vec2[2] result[2] = vec1[0] * vec2[1] - vec1[1] * vec2[0] return result if __name__ == "__main__": # Example vectors vector1 = [1.0, 2.0, 3.0] vector2 = [4.0, 5.0, 6.0] # Calculate and display the dot product dot_result = VectorOperations.dot_product(vector1, vector2) print("Dot Product:", dot_result) # Calculate and display the cross product cross_result = VectorOperations.cross_product(vector1, vector2) print("Cross Product:", cross_result)

C#

using System; using System.Collections.Generic; class Program { // Function to calculate the dot product of two vectors static double DotProduct(List<double> vec1, List<double> vec2) { double result = 0; for (int i = 0; i < 3; ++i) { result += vec1[i] * vec2[i]; } return result; } // Function to calculate the cross product of two vectors static List<double> CrossProduct(List<double> vec1, List<double> vec2) { List<double> result = new List<double> { 0, 0, 0 }; result[0] = vec1[1] * vec2[2] - vec1[2] * vec2[1]; result[1] = vec1[2] * vec2[0] - vec1[0] * vec2[2]; result[2] = vec1[0] * vec2[1] - vec1[1] * vec2[0]; return result; } static void Main() { // Example vectors List<double> vector1 = new List<double> { 1.0, 2.0, 3.0 }; List<double> vector2 = new List<double> { 4.0, 5.0, 6.0 }; // Calculate and display the dot product double dotResult = DotProduct(vector1, vector2); Console.WriteLine("Dot Product: " + dotResult); // Calculate and display the cross product List<double> crossResult = CrossProduct(vector1, vector2); Console.Write("Cross Product: "); foreach (double value in crossResult) { Console.Write(value + " "); } Console.WriteLine(); } }

Javascript

// Function to calculate the dot product of two vectors function dotProduct(vec1, vec2) { let result = 0; for (let i = 0; i < 3; ++i) { result += vec1[i] * vec2[i]; } return result; } // Function to calculate the cross product of two vectors function crossProduct(vec1, vec2) { let result = [0, 0, 0]; result[0] = vec1[1] * vec2[2] - vec1[2] * vec2[1]; result[1] = vec1[2] * vec2[0] - vec1[0] * vec2[2]; result[2] = vec1[0] * vec2[1] - vec1[1] * vec2[0]; return result; } // Example vectors const vector1 = [1.0, 2.0, 3.0]; const vector2 = [4.0, 5.0, 6.0]; // Calculate and display the dot product const dotResult = dotProduct(vector1, vector2); console.log("Dot Product: " + dotResult); // Calculate and display the cross product const crossResult = crossProduct(vector1, vector2); console.log("Cross Product: " + crossResult.join(" "));


Output

Dot Product: 32 Cross Product: -3 6 -3

Basic Geometry for Competitive Programming

Ever wondered how to tackle tricky problems in competitive programming? Well, basic geometry is your secret weapon! In this article, we’re diving into the basics Geometric Algorithms. It’s like a power-up for your problem-solving skills, giving you the tools to crack those coding challenges like a pro.

Table of Content

  • What are Geometric Algorithms?
  • Why to use Geometric Algorithms?
  • Vector Addition/Subtraction
  • Dot Product and Cross Product
  • Distance from a point to a line
  • Intersection Point of two lines
  • Practice Problems on Basic Geometry for Competitive Programming

Similar Reads

What are Geometric Algorithms?

Geometric algorithms are a set of computational techniques used in Competitive Programming to solve problems related to geometry and spatial relationships. These problems often involve points, lines, polygons, and other geometric objects....

Why to use Geometric Algorithms?

There are several questions which require basic geometric algorithms like:...

1. Vector Addition/Subtraction:

Operations like addition and subtraction of two vectors can be simply done by performing the operation on the individual components of the vectors. Like if we have two vectors (x1, y1) and (x2, y2), then the sum of the two vectors is (x1+x2, y1+y2) and the difference between them is (x1-x2, y1-y2)....

2. Dot Product and Cross Product:

The dot product of two vectors is simply the sum of the products of the corresponding elements. If we have two vectors (x1, y1) and (x2, y2), then the dot product of the two vectors is (x1 * x2) + (y1 * y2). The dot product of two vectors is a scalar quantity....

3. Distance from a point to a line:

Suppose we are given point A and a line L and we need to calculate the distance between the point and the line (say h), we can simply do it using Cross Product of vectors....

4. Intersection Point of two lines:

We can find the intersection point of two lines in 2D using parametric equations. Parametric equations are a way of representing a curve in terms of one or more parameters. For a line in 2D, we can use the following parametric equation:...

Practice Problems on Basic Geometry for Competitive Programming:

Problem Problem Link Check if two line segments intersect Practice Now Line Passing through two points Practice Now Find the missing point of parallelogram Practice Now...