lps9 22BPS1114
lps9 22BPS1114
lps9 22BPS1114
Aditya
22BPS1114
Q1. Given the two end points P1, P2 of a line segement P1P2, design an algorithm to
generate n points on P1P2. Your algorithm should get the input n and accordingly output
the x-coordinate, y-coordinate of all the n points. Analyse your algorithm with the time
complexity.
Answer:-
Problem Scenario:
Algorithm:
C++ Code:-
#include <iostream>
#include <vector>
#include <iomanip>
struct Point {
double x, y;
};
vector<Point> points;
return points;
int main() {
int n = 5;
cout << fixed << setprecision(2) << "(" << p.x << ", " << p.y << ")" << endl;
return 0;
Output:
(0.00, 0.00)
(2.50, 2.50)
(5.00, 5.00)
(7.50, 7.50)
(10.00, 10.00)
Time Complexity:
Q2. Given the two end points P1, P2 of a line segement P1P2, and the coordinates of a
point q, design an algorithm to check whether the point q in the clockwise direction from
the line segment P1P2 or not. Analyse your algorithm with the time complexity.
Answer:-
Problem Scenario:
Algorithm:
C++ Code:
#include <iostream>
struct Point {
double x, y;
};
double crossProduct = (P2.x - P1.x) * (q.y - P1.y) - (P2.y - P1.y) * (q.x - P1.x);
int main() {
cout << "Point q is " << checkClockwise(P1, P2, q) << " relative to P1P2." << endl;
return 0;
Output:
Time Complexity:
Answer:-
Problem Scenario:
Given the coordinates of nnn points in a 2D plane, identify all sets of collinear points.
Algorithm:
1. For each pair of points (P1,P2)(P1, P2)(P1,P2), compute the slope mmm as
(y2−y1)/(x2−x1)(y2 - y1) / (x2 - x1)(y2−y1)/(x2−x1).
C++ Code:
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <cmath>
using namespace std;
struct Point {
int x, y;
};
vector<vector<Point>> collinearSets;
collinear.push_back(points[k]);
if (collinear.size() > 2) {
collinearSets.push_back(collinear);
}
}
return collinearSets;
int main() {
vector<Point> points = {{1, 1}, {2, 2}, {3, 3}, {4, 5}};
cout << "(" << p.x << ", " << p.y << ") ";
return 0;
Input:
Output:
Collinear Sets:
Time Complexity:
Time Complexity: O(n3)O(n^3)O(n3), due to nested loops for each combination.