You are on page 1of 5

1

ModernProgrammingConceptsinEngineering

Modern Programming Concepts in Engineering


Assignment
-Sarat Maharjan 10801126118

1. Tasks to be done: Implement Polyline Class (View3D Package) to draw triangles. Include area, center of gravity, vertex labels and side labels with the respective triangle. Find Intersection points between two triangles. Draw Intersecting Polygon and find its C.G. and area. 2. Package Used: View3D Package: It is simple but powerful 3D graphics package developed by Matthias Baitsch. It is based on the Visualization Toolkit "VTK"and provides classes to represent various types of three-dimensional geometric entities. 3. Classes Used: TriangleDemo: To give inputs and receive the results from other classes and perform general tasks. Triangle: Creates triangles from three points Implemented Methods: - draw: Draws the triangle in the viewer. - addSideNames: Adds name of sides of triangle in the viewer. - findCentreOfGravity: Calculates centre of gravity of triangle. - findArea: Calculates the area of the triangle. - writeArea: Displays the area of triangle in the viewer. - createLines: Creates lines for sides of triangle. - findIntersection: Finds intersecting points with another triangle. - checkPointIsInTriangle: Checks if any point lies within triangle. Line: Creates line from two given points. Implemented Methods: - findEquation: Calculates slope and y intercept of the line. - getMidpoint: Calculates mid-point of the line. - isIntersecting: Checks if line intersects with another line. - findIntersection: Finds the intersection point with another line. - checkLimit: Checks if any point lies in the given length of line. - findAngle: Finds the angle of line with another line. CompEng|RUB

ModernProgrammingConceptsinEngineering

Point: Creates point with given co-ordinates Implemented Methods: - getX: Returns x-coordinate of the point. - getY: Returns y-coordinate of the point. - getZ: Returns z-coordinate of the point. - getCoordinates: Returns all coordinates of the point. - setCoordinates: Resets the position of Point with given coordinates - draw: Adds the point in the viewer to display. - print: Prints the coordinates in the console. - getDistance: Calculates the distance with any given point.

Polygon: Implemented Methods: - removeDuplicate: Removes duplicate entries from the vertexes. - findAreaOfPolygon: Calculates the area of Polygon. - findCentreofGravity: Calculates the center of gravity of the polygon. - reOrganize: Re-arranges the vertexes in anticlockwise manner. - draw: Adds the Polygon in the given viewer.

4. Prerequisite (Mathematical Background): Equation of Lines: - A straightlineon thecoordinateplanecan be described by the equation: y = mx + c; where m is theslopeofthelineand c is the intercept. - For the line parallel to y-axis, equation reduces to x=a; and for line parallel to x-axis, equation reduces to y=c. Calculation of angles between two lines (vectors):

The dot product of vectors isrelated to theirlength and angle. From which we can obtain the angle between two vectors:

where; a and b are the vectors and |a| and |b| denotes thelength(magnitude) of the vectors and is theanglebetween them. - And cross product of two vectors a X b = x1y2-y1x2 can be used to determine the direction of angle.

CompEng|RUB

ModernProgrammingConceptsinEngineering

Calculation of distance between two points: Given the two points (x1, y1) and (x2, y2), the . distance between these points is given by the formula: Check if any point lies within the triangle: Construct three triangles with the point to check and two vertexes of the given triangle. If the sum of three constructed triangles is equal to the area of the given triangle, the point lies within the triangle. To get the intersection of two straight lines, the lines can be used as system of linear equations and solution obtained is the intersection point. The Intersection points of the two triangles are always the vertex of convex hull, so if they are plotted in clockwise or anticlockwise direction with respective to point within the convex hall (here centre of gravity is used) will give the required intersection polygon. The area of triangle is given by 0.5*|((x1-x3)(y2-y1)-(x1-x2)(y3-y1))| where (x1,y1), (x2,y2) and (x3,y3) are the vertexes of the triangle. This is reduced formula for area of polygon which is not self intersecting. The area of polygon, not self intersecting and points taken in sequence is given by:

Here, n-1 is the last vertex. And similarly the centre of gravity of polygon is given by following formula:

where C.G. =(cx,cy)

CompEng|RUB

ModernProgrammingConceptsinEngineering

5. Some Screenshots of Results:

CompEng|RUB

ModernProgrammingConceptsinEngineering

5. Conclusion Assignment is done with full dedication; and efforts have been made to minimize the errors as much as I can. Even so, I cannot be sure that all I have done is free of error. So, I expect comments and suggestions from you. Last but not least, I thank you for your guidance to help us understand Modern Programming Concepts, and I am sure this will be very helpful for our academic and professional career.

Sincerely, Sarat Maharjan

CompEng|RUB

You might also like