Sandhi Daa Lab Da-5
Sandhi Daa Lab Da-5
Registration Number:
22BCE2980
Date: 14/04/2024
Exercises to do:
1. Write a C-program/ C++ program to find the
convex hull using Graham’s Scan algorithm with at
least 6 nodes with one or more collinear and right
turn points.
ALGORITHM:
GRAHAM-SCAN(Q)
1. Let po be the point with the minimum y-
coordinate or the leftmost such point in case of a
tie
2. let (P1, P2, ..., Pn) be the remaining points in Q,
sorted by polar angle in counter clockwise order
around po (If more than one point has the same
angle, remove all but one that is farthest from po)
3. Let S be an empty stack
4. PUSH(po, S)
5. PUSH(P1, S)
6. PUSH(P2, S)
7. for i3 to n
8. while ccw(next-top(S), p₁, top (S)) ≤ 0 Pi
9. POP(S)
10. PUSH(S)
11. return S
The clockwise and counter clockwise algorithm
ccw(P1, P2, P3) CCW
return (p3-P1 P2-P1)
C/C++ Code:
#include <iostream>
#include <stack>
#include <stdlib.h>
struct Point
{
int x, y;
};
Point p0;
S.pop();
S.push(p);
return res;
}
p1 = p2;
p2 = temp;
}
if (val == 0) return 0;
if (o == 0)
int y = points[i].y;
swap(points[0], points[min]);
p0 = points[0];
int m = 1;
points[i+1]) == 0)
i++;
points[m] = points[i];
m++;
if (m < 3) return;
stack<Point> S;
S.push(points[0]);
S.push(points[1]);
S.push(points[2]);
S.pop();
S.push(points[i]);
while (!S.empty())
Point p = S.top();
cout << "(" << p.x << ", " << p.y <<")" << endl;
S.pop();
}
}
int main()
{
Point points[] = {{0, 3}, {1, 1}, {2, 2}, {4, 4},
convexHull(points, n);
return 0;
}
OUTPUT:
RESULT:
C++ program to find the convex hull using
Graham’s Scan algorithm implemented
successfully.
VERIFICATION STATUS:
2. Write a C-program/ C++ program to find the
convex hull using Jarvis March algorithm with at
least 6 nodes with one or more collinear.
ALGORITHM:
JARVISMARCH(X[1..n],Y[1..n]):
1. l←1
2. for i←2 to n
a. if X[i] < X[l]
i. l←i
3. p←l
4. repeat
a. q←p+1 ((Make sure p ≠
q))
b. for i←2 to n
i. if CCW(p, i, q)
1. q←i
ii. next[p] ←q; prev[q] ← p
iii. p ← q
5. until p = l
C/C++ Code:
#include <bits/stdc++.h>
struct Point
{
int x, y;
};
if (val == 0) return 0;
if (n < 3) return;
vector<Point> hull;
int l = 0;
l = i;
int p = l, q;
do
{
hull.push_back(points[p]);
q = (p+1)%n;
q = i;
p = q;
} while (p != l);
int main()
{
Point points[] = {{0, 3}, {2, 2}, {1, 1}, {2, 1},
int n = sizeof(points)/sizeof(points[0]);
convexHull(points, n);
return 0;
}
OUTPUT:
RESULT:
C++ program to find the convex hull using Jarvis
March algorithm implemented successfully.
VERIFICATION STATUS:
3. Write a C-program/C++ program to perform the
intersection of line segments algorithm.
Case1: At least 2 lines with intersection.
Case2: At least 2 lines without intersection.
Case3: At least 2 lines with Collinearity.
ALGORITHM:
SEGMENTS-INTERSECT (P1, P2, P3, P4)
1. d₁ =DIRECTION (P3, P4, P1)
2. d2 =DIRECTION (P3, P4, P2)
3. d3 =DIRECTION (P1, P2, P3)
4. d4 =DIRECTION (P1, P2, P4)
5. if ((d₁>0 and d₂<0) or (d₁<0 and d2>0)) and
((d3>0 and d₁<0) or (d3<0 and d₁ >0))
6. then return TRUE
7. elseif d₁ = 0 and ON-SEGMENT(P3, P4, P1)
8. then return TRUE
9. elseif d₂ = 0 and ON-SEGMENT (P3, P4, P2)
10. then return TRUE
11. elseif d3 = 0 and ON-SEGMENT (P1, P2, P3)
12. then return TRUE
13. elseif d4 = 0 and ON-SEGMENT (P1, P2, P4)
14. then return TRUE
15. else return FALSE
DIRECTION (Pi, Pj, Pk)
return (pk-Pi) x (Pj-Pi)
ON-SEGMENT (Pi, Pj, Pk)
if min(xi, xj) ≤ x ≤ max(xi, xj) and min(yi, yj) ≤ y ≤
max(yi, yj)
then return TRUE
else return FALSE
C/C++ Code:
#include <iostream>
#include <cmath>
struct Point {
double x, y;
};
int main() {
// Case 1: At least 2 lines with intersection
Point intersection;
if (segmentsIntersect(0, 0, 2, 2, 0, 2, 2, 0, intersection)) {
cout << "Intersection point: (" << intersection.x << ", " <<
intersection.y << ")" << endl;
} else {
cout << "No intersection" << endl;
}
return 0 ;
}
OUTPUT:
RESULT:
C++ program to perform the intersection of line
segments algorithm implemented successfully.
VERIFICATION STATUS: