0% found this document useful (0 votes)
9 views7 pages

Practical 5

The document outlines practical applications of computational geometry using Jupyter Notebook, focusing on lines, segments, and rays. It includes examples of defining lines, calculating angles, intersections, lengths, distances, slopes, midpoints, and transformations of linear entities. Additionally, it demonstrates the use of the SymPy library for geometric computations and includes error handling for undefined attributes.

Uploaded by

adwaitdorke
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views7 pages

Practical 5

The document outlines practical applications of computational geometry using Jupyter Notebook, focusing on lines, segments, and rays. It includes examples of defining lines, calculating angles, intersections, lengths, distances, slopes, midpoints, and transformations of linear entities. Additionally, it demonstrates the use of the SymPy library for geometric computations and includes error handling for undefined attributes.

Uploaded by

adwaitdorke
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

3/3/25, 11:46 AM Practical 5 - Jupyter Notebook

Practical 5: Application to Computational


Geometry

2.3 Lines
In [22]: 1 #Two points that lie on the same line are said to be collinear points
2 #Line, L passing through the points(2,3) and (4,3) is defined as follow
3 ​
4 L = Line(Point(2,3), Point(4,3))

Out[22]: Line2D(Point2D(2,3),Point2D(4,3))
In [24]: 1 #The line corresponding to an equation in the form of ax + by + c = 0 c
2 #Line having equation 2x+3y=4:
3 P = Line(2*x + 3*y - 4 )
4 P

Out[24]:
Line2D(Point2D(0, 43 ),Point2D(1, 23 ))
In [63]: 1 #The line with point and slope can be entered as follows:
2 #Line with Point (1, 4) and slope=1
3 B = Line(Point(1,4), slope = 1)
4 B

Out[63]: Line2D(Point2D(1,4),Point2D(2,5))
In [ ]: 1 #We can display equation and coefficients as follows:

In [64]: 1 B.equation()

Out[64]: −𝑥 + 𝑦 − 3
In [68]: 1 B.coefficient()
2 ​
3 #ID and fix error later

--------------------------------------------------------------------------
-
AttributeError Traceback (most recent call las
t)
Cell In[68], line 1
----> 1 B.coefficient()

AttributeError: 'Line2D' object has no attribute 'coefficient'

Line Segment

localhost:8888/notebooks/Practical 5.ipynb# 1/7


3/3/25, 11:46 AM Practical 5 - Jupyter Notebook

In [69]: 1 S = Segment((0,0),(0,1))
2 S

Out[69]: Segment2D(Point2D(0,0),Point2D(0,1))
Ray
In [70]: 1 r = Ray ((0,0), (2,3))
2 r

Out[70]: Ray2D(Point2D(0,0),Point2D(2,3))
Functions related to lines, segments and
rays:
1. Angle between rtwo linear entities(Lines, rays, segment): We can find angle formed by
intersection of linear entities using angle_between as follows

In [71]: 1 l = Line((0,0), (0,3))


2 s = Segment((0,0), (0,1))
3 r = Ray((0,0),(8,8))

In [73]: 1 # Angle between Line l and Segment s


2 l.angle_between(s)

Out[73]: 0
In [75]: 1 # Angle between Ray r and Segment s
2 r.angle_between(s)
𝜋
4
Out[75]:

In [76]: 1 # Angle between Ray r and Line l


2 r.angle_between(l)

Out[76]: 𝜋
4
2. Intersection if points of two linear points:

In [4]: 1 from sympy import *


2 ​
3 l = Line((0,8),(8,0))
4 s = Segment ((0,0),(10,10))
5 r = Ray((0,3),(3,0))

localhost:8888/notebooks/Practical 5.ipynb# 2/7


3/3/25, 11:46 AM Practical 5 - Jupyter Notebook

In [5]: 1 #from sympy import *


2 ​
3 l.intersection(s)

Out[5]: [Point2D(4, 4)]

In [6]: 1 #from sympy import *


2 ​
3 l.intersection(r) #it showed empty because there's no intersection

Out[6]: []

3. Length Linear entities:

In [7]: 1 """
2 from sympy import *
3 ​
4 l = Line((0,8),(8,0))
5 s = Segment ((0,0),(10,10))
6 r = Ray((0,3),(3,0))
7 """
8 ​
9 s.length

Out[7]: 10√⎯⎯2
In [9]: 1 l.length

Out[9]: ∞
In [10]: 1 r.length

Out[10]: ∞
4. Distance:

In [11]: 1 """from sympy import *


2 ​
3 l = Line((0,8),(8,0))
4 s = Segment ((0,0),(10,10))
5 r = Ray((0,3),(3,0))"""
6 p = Point(10,10)
7 ​

In [12]: 1 l.distance(p)

Out[12]: 6√⎯⎯2
In [13]: 1 s.distance(p)

Out[13]: 0

localhost:8888/notebooks/Practical 5.ipynb# 3/7


3/3/25, 11:46 AM Practical 5 - Jupyter Notebook

In [14]: 1 r.distance(p)

Out[14]: 17√⎯⎯2
2
5. Slope of the linear entities

In [26]: 1 from sympy import *


2 ​
3 l = Line((0,0),(2,4))
4 l.slope ##Slope of Line l

Out[26]: 2
In [17]: 1 #from sympy import *
2 s = Segment ((1,0),(2,-1))
3 s.slope #Slope of Seg s

Out[17]: −1
In [19]: 1 #from sympy import *
2 r = Ray((0,0),(3,3))
3 r.slope #Slope of Ray r

Out[19]: 1
6. Midpoint of the segment:

In [21]: 1 #from sympy import *


2 s = Segment ((1,0),(2,-1))
3 s.midpoint

Out[21]:
Point2D( 32 ,− 12 )
In [22]: 1 #from sympy import *
2 s = Segment ((3,2),(2,-3))
3 s.midpoint

Out[22]:
Point2D( 52 ,− 12 )
7. Point of linear entities:

localhost:8888/notebooks/Practical 5.ipynb# 4/7


3/3/25, 11:46 AM Practical 5 - Jupyter Notebook

In [1]: 1 #from sympy import *


2 #from math import *
3 l = Line(x+y-5)
4 l.points()

--------------------------------------------------------------------------
-
NameError Traceback (most recent call las
t)
Cell In[1], line 3
1 #from sympy import *
2 #from math import *
----> 3 l = Line(x+y-5)
4 l.points()

NameError: name 'Line' is not defined

In [33]: 1 r = Ray((0,0),(0,1))
2 r.points

Out[33]: (Point2D(0, 0), Point2D(0, 1))

In [40]: 1 #from sympy import *


2 #from math import *
3 ​
4 x, y = symbols('x y') # Define x and y as symbolic variables
5 ​
6 L1 = Line(x+y-5)
7 print(L1.points) # This will likely output an empty list as Line doesn'

(Point2D(0, 5), Point2D(1, 4))

8. Rotation of linear entities:

In [41]: 1 l = Line((0,0),(0,1))
2 l.rotate(pi/6)

Out[41]:
Line2D(Point2D(0,0),Point2D(− 12 , 1000000000000000
866025403784439
))
In [43]: 1 #from sympy import *
2 a = l.rotate(pi/6)
3 a.equation()

Out[43]:
− 866025403784439𝑥 −
1000000000000000 2
𝑦
In [44]: 1 s = Segment((1,0),(2,-1))
2 s.rotate(pi)

24492935982947
Segment2D(Point2D(−1, 200000000000000000000000000000 ),Point2D(−2,1))
Out[44]:

localhost:8888/notebooks/Practical 5.ipynb# 5/7


3/3/25, 11:46 AM Practical 5 - Jupyter Notebook

In [45]: 1 r = Ray((0,0),(4,4))
2 r.rotate(pi/2)

Out[45]: Ray2D(Point2D(0,0),Point2D(−4,4))
In [46]: 1 r.rotate(-pi/2) #rotate by -90 degrees

Out[46]: Ray2D(Point2D(0,0),Point2D(4,−4))
In [47]: 1 r.rotate(pi) #rotate by 180 degrees

Out[47]: Ray2D(Point2D(0,0),Point2D(−4,−4))
9. Rotation of linear entities

In [48]: 1 x, y = symbols("x y")


2 a = Line(4*x+3*y-5)
3 b = Line(x+y)
4 ​
5 #Reflection of ```a``` through ```b```
6 c = a.reflect(b)
7 c.equation()
8 #Line = a.reflect(b)
9 #Line.equation()
10 ​

Out[48]:
𝑥 + 4𝑦3 + 53
Ex: 2.12 Reflect the segment through two endpoints [(2,3),(4,6)] through line 7x+6y =
3

In [50]: 1 ​
2 #from sympy import *
3 A = Point(2,3)
4 B = Point(4,6)
5 ​
6 S = Segment(A, B)
7 x,y = symbols("x y")
8 l = Line(7*x + 6*y - 3)
9 ​
10 #Reflection of "S" through "l"
11 ​
12 S.reflect(l)

Out[50]:
Segment2D(Point2D(− 236 ,− 93 ,Point2D − 514 ,− 222
85 85 ) ( 85 85 ))
Ex: 2.13 Reflect the line segment having starting Point[(0,0)] in the direction of (2,4)
through line x - 2y = 3

localhost:8888/notebooks/Practical 5.ipynb# 6/7


3/3/25, 11:46 AM Practical 5 - Jupyter Notebook

In [9]: 1 ​
2 from sympy import *
3 A = Point(0,0)
4 B = Point(2,4)
5 ​
6 S = Ray(A, B)
7 x,y = symbols("x y")
8 l = Line(x - 2*y - 3)
9 ​
10 #Reflection of Ray "S" through Line "l"
11 ​
12 S.reflect(l)

Out[9]:
Ray2D(Point2D( 65 ,− 125 ),Point2D( 285 ,− 165 ))
10. Transformation of linear entities:

Ex. 2.14: If the line with points A[2 1] , B[4 -1] is transformed by the transformation
matrix, [T] = Matrix([[1,2],[2,1]]) , then find the equation of transformed line.

In [6]: 1 #Matrix of reflection in Python will be in 3x3 matrix. i.e Matrix([[1,2


2 ​
3 A = Point(2,1)
4 B = Point(4,-1)
5 ​
6 A1 = A.transform(Matrix([[1,2,0],[2,1,0],[0,0,1]]))
7 B1 = B.transform(Matrix([[1,2,0],[2,1,0],[0,0,1]]))
8 ​
9 L = Line(A1, B1)
10 L.equation()

Out[6]: −2𝑥 − 2𝑦 + 18

localhost:8888/notebooks/Practical 5.ipynb# 7/7

You might also like