A Method of Determining the Direction of Two Dimensional Curves F. Wang1, a, R.K.F. Abdelmaguid2, b and H.M.A. Hussein3, c 1
Jiangxi College of Solar Energy Technologies, Jiangxi 33800, China
2
Beni-Suef University, Production Technology Department, Faculty of Industrial Education, Beni-Suef, Egypt 3
Advanced Manufacturing Institute, King Saud University, 11421, Riyadh, Saudi Arabia a
[email protected],
[email protected],
[email protected]
Keywords: Computer algorithms. Computational geometry. Direction of two-dimensional curves.
Abstract. Two-dimensional curves are represented by a list of vertices and other parameters that control the shape or curvature of the segments. In computer programming to deal with closed two-dimensional curves, it is often required to know the direction of the curve, which is reflected by the sequence of the vertex data. It can be anticlockwise or clockwise. This paper presents a robust, linear algorithm to determine the direction of a closed two-dimensional curve, by computing the total angular change of a tangent vector travelling along the curve for a complete cycle. A new, robust linear algorithm is proposed for the determination of the positional relationship of a point to a two-dimensional curve. For curves that consist of line and arc segments, which are most commonly used in engineering applications in computer aided design, the paper presents algorithms and procedures for solving the above problems. Introduction Two-dimensional curves are important entities widely used in computer aided design (CAD) systems and desktop publishing (DTP) systems. Typically, two-dimensional curves are defined as polylines and splines in CAD systems. These are very commonly used by industrial engineers for their amazing properties. Polyline is the most commonly used entity because profiles consisting of lines and arcs are easy to design and manufacture. A closed polyline can be used to define the profile of a part, the shape of a pocket or a hole on a plate, or an AEC column, brace, or beam etc in architectural design. Normally, a polyline consists of a number of lines and arcs segments. Once defined, those lines and arcs are tied together as one object unless the user purposely "explodes" it. Internally a CAD system defines a polyline entity by recording a number of vertices and bulge factors, which is another form of the included angle of arcs. To automate computer aided design tasks, it is useful to develop necessary computer algorithms of two-dimensional curves. This paper deals within the method of determining the direction of a closed two-dimensional curve. The algorithms are implemented on the AutoCAD platform. It will provide basis for many other geometric algorithms. Definition of The Problem The Direction of Two-dimensional Curves. Normally, a two-dimensional curve is created in AutoCAD in an interactive manner. It is observed that the internal records of the vertices of a polyline may be clockwise or anticlockwise. This has caused greater complexity when processing the data of polylines: one always needs to generate two branches of his computer codes to consider two situations, one for clockwise direction and other being for anti-clockwise direction of the profile. It is thus necessary to have a fundamental algorithm to determine the direction of a two-dimensional curve. Once a curve is found to be clockwise (negative direction), the vertices and the bulge factors may be reversed properly so that only curve data of "positive" direction (normally anticlockwise) will be 81
generated for other applications. In the downstream applications, one then needs to consider only the situation of one direction. For the definition of the curve direction, see Fig. 1. A two-dimensional curve, C, being smooth for each of its segments, encloses a single region, B. When one travels along the curve, if region B is on his left-hand side, it is said that the direction is positive (+); if the region is on one's right-hand side, the direction is negative (-).
Fig. 1. Direction of a two-dimensional curve. Related Work Computational Geometry. Geometry is an ancient science in human history that, together with algebra played important roles to the development of other sciences. Computational geometry is a young science started in the 70s as the use of computers became popular. Excellent reviews of the field can be found in [3-4]. Shamos [5-6] was a pioneer of this field and his PhD thesis treated a large number of geometric algorithms, which stimulated much of later research. Eventually the book of Preparata and Shamos [7] became the classic reference of the domain. The field is developing quickly. Edelsbrunner [8] describes many results of the 80s. The classic problems include intersections, simple closed path, inclusion of polygons, convex hull, range searching, and closest-point problem. Algorithms of these problems serve the foundation for computer aided design and other computer graphics applications. Boissonnat [9], and Baerentzen [10] describes the recent demand of the new concepts of computational geometry in our life. Direction of Two-dimensional Curves. Strictly speaking, there is no direction for a closed two-dimensional curve. However, when vertices and other shape parameters are used to represent the curve, there is a sequence issue, and thus there is always a direction issue. It is caused by the sequence of the vertices. No previous work has been found to investigate this problem to the best of the authors. Algorithm for Determining Curve Direction Basic Principle of Determining the direction of a Two-Dimensional Curve. Let's consider a generic two-dimensional curve C on a plane. It will meet with the following conditions: (1) Being smooth within each segment; (2) Not having self-intersections; (3) Being closed, i.e., it’s first and its last vertices are at the same position. As a result, the curve encloses a single region, B, on the X-Y plane. If a particle travels along the curve when the enclosed region is on the left-hand side, we call the direction positive or anticlockwise; otherwise, it is negative or clockwise. To determine the direction, we introduce a vector, r, at a point p(x,y) on the curve, see Fig.2. The magnitude of the vector is not of concern. Its direction is along the tangent of the curve at point p. The vector is called Tangent Vector (TGV) of the curve. When p travels along the trace of a curve the tangent vector moves with the point at a direction always following the tangent of the curve at the point p. We denote the variable of the angle of the vector as φ(x,y) (0≤ α ≤ 2π), with zero at the right direction along with the x-axis. When point p moves from p(x,y) to a nearby point p(x+Δx, y+Δy), there is an change in the angle of the vector, Δ φ = φ(x + Δx,y + Δy) – φ(x,y) 82
We call this angle the Swept Angle of the Tangent Vector (SAT) from point p(x,y) to p(x+Δx, y+Δy). When Δφ→0, we use directive notation for the incremental swept angle, dφ . In fact, φ= dy/dx, the directive of the y to x. Then, we perform integration along the closed curve, getting:
d
(1)
c
Lemma 1: A closed two-dimensional curve, C, forms a single region, B. When a point p travels continuously alone the curve with the enclosed region being on the left hand side of the point, from a start point P0 all the way back to the start point, the integral of the swept angle of the tangent vector, φ, will be 2π. Otherwise, when the point p travels continuously along the curve with the region being on its right-hand side, from a start point P0 all the way back to the start point, the integral value of φ will be -2π. Proof: In the current problem, we do not concern about the position of the vector. What is important in the calculation is the change in angle of the vector. As such, at any point p(x, y), we can shift the vector to one point Pi inside of the curve, see Fig.3. When the point travels along the curve and the vector moves with the point, the problem is equivalent to a problem of rotating a vector around point Pi
Fig. 2. Tangent Vector of curve C and its angle
Fig. 3. Total swept angle of tangent vector.
Properties of the Swept Angle for Tangent Vector. Before we present the implementation of the algorithm, we need to show some interesting properties of the swept angle for the tangent vector of a closed curve. 1. For a smooth segment of a curve, as long as it is not self-intersected, the swept angle of tangent vector is the difference between the vector angle at the exit point and that at the entry point, and it is independent of the path of the curve. This property is obvious. It can be proven in the same way as proving Lemma 1 by considering that the swept angle has nothing to do with position of the vector. Whenever the tangent vector is changing continuously, the total change in angle is only determined by the end and start angles of the vector. With this property, the swept angle of any curves can be easily calculated, being spline, or any other functional form,
g e s
(2)
where g stands for the swept angle over the current segment, s and e for vector angles at the start and end of the current segment, respectively. 2. At the common endpoint of two segments, each of which is smooth, the swept angle is calculated by subtracting the tangent angle of 2nd segment at the start point and the tangent angle of first segment at the end point,
v v v
(3)
where; v is the swept angle at the vertex v, v the vector angle at the start of next segment, and v the vector angle at the end of the previous segment. This property is obvious due to the definition of the SAT. 83
Implementation of the Algorithm Tangent Angle of Arc at Start and End Points. Let the start point of an arc, A, be Ps, and end point Pe, the tangent angle at the start point is calculated by
s se / 2
(4)
where se is the angle from point Ps to Pe, the angle of the chord, in the range of 0 and 2π (see Fig. 4 for its definition).
Fig. 4. Start and end angles of tangent vector The variable α in Eq.(4) is the included angle of the Arc. When bulge factor, β is used in a CAD system internally for the arc's shape, the relationship between the bulge factor and the included angle, α is given by,
4 arctan( )
(5)
Likewise, the tangent angle at the end point of the arc is calculated from
e se / 2
(6)
It should be noted that when applying property 1 of Swept Angle of a smooth curve, given above, indicating the correctness of these formulae. In reality, the swept angle of TGV along an arc has no need to compute as it can be obtained directly from the data that defines the arc, for example, from the bulge factor.
e s ( se / 2) ( se / 2)
(7)
Swept Angle of Tangent Vector at a Vertex. The second property of swept angle of TGV can be followed. Let's consider a vertex at which two segments, Arc A1 and Arc A2, are to be connected. This is the most complicated case. Any one or both of them can be a line. The principle of the computation remains the same. Let the angle of TGV at the end of A1 be 1e , and the angle of TGV at the start of A2 be, 2e , Then, the swept angle of TGV at the this vertex vi will be,
i 2 s 1e
(8)
For a line edge, the angles of TGV at both the start and the end vertices are the same, and are equal to the angle from start to end vertex. Implementation of the Algorithm. When a curve consists of lines and arcs, the curve can be internally represented by two series of parameters:
Pi ( x, y )i 0,1,2,......N 1
i or i {i 0,1,2,......N 1}
84
where Pi is a vertex at point i, i is the bulge factors of the arc for the ith segment. It can be converted to include angle i by Eq. 5. The total swept angle is calculated using the following procedure 1. Transform the CAD internal data to two series table, Pi and i {i =1,2,…N-1}; 2. Compute the Swept Angle at each vertex i {i = 1,2…N-1} according to Eq.8. 3. Add all Swept Angles at the vertices together, resulting in
:
N 1
v i
(9)
i 1
4. Add all the included angles together, resulting in : N 1
i
(10)
i 1
5. Add the swept angle of vertex and that of the arc v | a
(11)
6. Output the conclusion according to the value of Ф. Summary One robust algorithm is presented in this paper for the determination of the direction of a two-dimensional curve. The algorithm helps computer programmers to significantly simplify their application codes for processing two-dimensional curves. With the algorithm, one always simply considers his curve in the positive direction. In order to develop the algorithm, concepts of Tangent Vector of two-dimensional curves and its Swept Angle are introduced. The direction of the curve is judged by the value of the total swept angle of the tangent vector. The second algorithm, which is based on the first algorithm, is used to determine the position of a point in relation to the curve. Concepts of Radial Vector and the Swept Angle of Radial Vector are introduced. Computations are made only on swept angles of tangent vector and radial vector in the algorithms. The first algorithm is a novel one. It uses a simple concept of integral of swept angle of tangent vector to determine the direction of a curve. Acknowledgment This work is supported by NSTIP strategic technologies programs, Grant number (12-INF2816-02) in the Kingdom of Saudi Arabia. References [1] F. Wang, SM-Design: SMCAD User’s Guide, Published by Gintic Institute of Manufacturing Technology, Singapore. (1998). [2] F. Wang, Development of a state-of-the-art CAD for sheet metal progressive dies: SMCAD, Proceedings of CAD/CAM/CAE for Internet & Intranet, Temasek Polytechnic, Singapore. (1998) 28-29. [3] P. Zhou, Editor, Algorithm Design and Analysis, Text book for higher education, Mechanical Engineering Publisher, Beijing, (1998). [4] R. Sedgewicw, Algorithms in C++, Addison-Wesley Pub. Company, Geometric Algorithms, pp. 347-401. 85
[5] M.I. Shamos, D. Hoey, Closest-point problems, presented in 16th, Annual Symposium on Foundations of Computer Sciences, IEEE. (1975). [6] M.I. Shamos, D. Hoey, Geometric intersection problems, presented in 17th Ann. Symposium on Foundations of Computer Sciences, IEEE. (1976). [7] F.P. Preparata, M.I. Shamos, Computational Geometry: An Introduction, Springer-Verlag. (1985). [8] H. Edelsbrunner, Algorithms in Combinatorial Geometry, Springer-Verger. (1987). [9] J.D. Boissonnat, M. Teillaud, Effective computational geometry for curves and surfaces, Springer-Verlag, Berlin, Heidelberg. (2006). [10 ] J.A. Baerentzen, J. Gravesen, F. Anton, H. Aanaes, Guide to computational geometry processing, Foundations, Algorithms, and Methods, Springer-Verlag, London. (2012).
86