geometry,coordinate-systems,divide-by-zero , Best way to avoid divide by zero while doing math on geometric shapes

Best way to avoid divide by zero while doing math on geometric shapes


Tag: geometry,coordinate-systems,divide-by-zero

I'm coding on a specialized motion controller hardware which uses a subset of Visual Basic language as its programming language. I need to make some calculations on given points, like getting center coordinates of an arc that is passing through 3 distinct points; shifting lines, getting intersection coordinates of 2 lines etc...

I'm using cartesian coordinate system for calculations. It seems that the code will get bloated while handling divide by zero cases.

So, what is the best coordinate system for such calculations? Or what is the best approach to keep such code small?


The code giving center point coordinates of an arc that is passing through 3 points is as follows:

    ' p1: (px1, py1)  p2: (px2, py2) p3: (px3, py3)
    ' ----------------------------------------------

    m_r=(py2-py1)/(px2-px1) ' slope of the line between p1 and p2
    m_t=(py3-py2)/(px3-px2) ' slope of the line between p2 and p3


As you can see, there are many cases that will cause a divide by zero exception. Should I use if/else structure or is there a better way?


When you calculate center point of an arc that is passing through 3 points, you definitely need to check if these points lies on the same line. But rewrite expression

if (py2-py1)/(px2-px1) = (py3-py2)/(px3-px2)

to avoid dividing

Det = (py2-py1) * (px3-px2) - (py3-py2) * (px2-px1) 
if Det = 0 then ... `bad case

for else case (Det<>0) arc exists, and you can find center coordinates without zero-divide errors

center_x= ((py2-py1) * (py3-py2) * (py3-py1) + 
          (py2-py1) *(px2+px3) * (px3-px2) - 
          (py3-py2) * (px1+px2) * (px2-px1)) / (2 * Det)


Trilateration algorithm to position 3 circles as close as possible without overlapping

I have two circles that are positioned perfectly on the boarder of each other. They have position A and B (both vectors), and radii Ra and Rb. Now I add a third circle with radii Rc. How can I find the position vector C where the three circles are placed...

rotating a square in javascript

I am trying to rotate a square about its center in javascript. Although in this case I am using canvas to draw the square, I need to have a functional method of rotating the square for use elsewhere so just rotating the canvas is not acceptable. This is what I...

Finding Points on a Quadratic Bezier Curve Path

I am attempting to create a shape like the following: For the curves of the spiral, I am utilizing Quadratic Bezier Segments. PathGeometry pg1 = new PathGeometry(); PathFigure pf1 = new PathFigure() { StartPoint = new Point(Convert.ToDouble(middle) + 500, Convert.ToDouble(middle) + 500) }; PathSegmentCollection psc1= new PathSegmentCollection(); QuadraticBezierSegment arcs1 =...

Reversing RotateAxisAngle back to angles

I'm trying to figure out how to reverse RotateAxisAngle to get back rotations around these arbitrary axes (or equivalent rotations that yield same net rotation, doesn't have to be identical). Does anyone know how to do it? I'm using MathGeoLib, but I don't see an opposite way, to return back...

Angle of a line on a certain point [closed]

I think the image says all. I have to find out the theta for any given line programatically with c#. Point X = new Point(x1,y1); Point Y = new Point(x2,y2); Now I want to draw a line from X point with theta angle. how can I implement with c#....

OpenGL fixed coordinate transformations

Was just trying to do some rendering with OpenGL and I happened to think about that: how would you run transformations in a fixed coordinate system. In other words, if you rotate an object around one axis you will see that the others are going to rotate too? Therefore, following...

2D perspective transform in JavaScript

Looking for a code example of how to calculate (accurately) the dimensions of a rectangle from a top down view (90 degrees) at a particular angle. e.g. I have a rectangle of 1200mm x 2000mm How would i determine the new perspective measurements of the rectangle if it was angled...

Detect hole in geometry

I am working with a serialization pipeline. I am taking a model and exporting it. I do not wish to export any model with a hole in a face. How would I detect a hole and report a error? I have access to all vertexes, edges, faces etc. Here is...

iOS: convert CGPoint in screen coordinates to CATransform3D in iCarousel?

I wanted to add a possibility to drag elements inside the carousel and I have some troubles with positioning and transformations of a draggable element. As I understand it is better to temporarily hide/delete the draggable element and create its proxy outside the carousel item array which represents the position...

Rotate line segment with Button

I have a line which has points (x1,y1) and (x2,y2). I wanted to attach a Button to it, it should align with the line by rotating based on the line segment points. I need some help in calculating the rotation angle for the Button.

How to convert a point in image imrotated with loose option to a point in image imrotated with crop option in Matlab?

How to find the relation between image imrotated with loose option Image 1 and image imrotated with crop option Image 2 ? the angle of rotation is not necessary -45° ... I = imread('cameraman.tif'); Im1 = imrotate(I,-45); % bbox option sets to 'loose' by default Im2 = imrotate(I,-45,'nearest','crop'); % bbox...

How to Draw Circle, incrementally, to create an animated fill effect (using Corona)

I'm having trouble even figuring out where to start with this. ANY help would be highly appreciated! Using the Corona SDK I want to draw a circle that will slowly fill as a percentage increases. The fill effect will follow the path of the circle, going anti-clockwise until the entire...

Find intersection of polygon and order by line

I was wondering if there is a method to order intersection points in a list based on the direction of the intersected line. Here is a picture to get the idea: The red numbers are the polygon lines which exist in a List. Then I have another List with lines...

How to check a point is inside an ellipsoid with orientation?

For an ellipsoid of the form with orientation vector and centre at point , how to find whether a point is inside the ellipsoid or not? An additional note that the geometry actually is with a=b (spheroid) and therefore one axis is sufficient to define orientation Note: I see a...

How is the Point2D “angle” method to be understood?

I thought I'd use the Point2D class for calculating e. g. the angle betwen 2 points instead of creating a utility method. It turned out, it's not usable at all. Code example: Point2D p1; Point2D p2; p1 = new Point2D(0, 0); p2 = new Point2D(10, 10); System.out.println( "Point2D: " +...

How do I enlarge a shape while ensuring the original will fit inside it concentrically?

I have islands in my game defined by an array of vertices. I would like to create a larger version of them to make a type of "shore", mainly for cosmetic purposes, while keeping the original shape intact to use for collision purposes. I have tried dilating it point by...

Distribute circles around a center circle

I'm trying to place six circles around a center circle. Eeach of them has the same diameter so it should be possible to place them around the center without space between or overlaps. I thinking to be close to the solution but there are small overlaps. I can't find an...

Linking borders of geometric figures

I'm creating an Android app which simulates petri nets. I'm representing place by circle and transition by square and I'm connecting those views by arc (it's just the name from petri nets for my purpose it's just a straight line) and here's the problem I'm facing, I know how to...

Line segment intersection

I found this code snippet on, however the link to the explanation wasn't valid anymore. I "translated" the answer into Swift, I hope you can understand, it's actually quite easy even without knowing the language. Could anyone explain what exactly is going on here? Thanks for any help. class...

how to convert Oracle geometry to SQL GEOMETRY

Ihave some geometry datatype entries in one of my table in a Oracle database.I would need to import those data into a sql server table to then use them for something else. I am aware that I cannot do that with an ssis package.There might also be some conversion problems.Could...

Convert PictureBox Selection Rectangle according to SizeMode

I have two PictureBoxes pbOriginal and pbFace. After Selecting a "face" from an image in pbOriginal I clone the rectangle selection and place it into pbFace. However, Because pbOriginal is using SelectionMode=Stretch the actual area being copied is not the same as the area being selected. How do I convert...

Angle between two lines beginning at one side of the line

I have two lines specified by two points with x and y coordinate each. The first point (where the lines begin) is equal meaning that I have 3 points A, B and C where one line is from A to B and the other one from A to C. I...

combining several iterations of same object in different location using .add() or .merge()

I am trying to make a coil with several small loops. I have a custom function to create a single helix for each loop, and at first I was calling that within a for loop several hundred times, but it was taking too long to render and slowed down the...

How can I make a symbol point at the mouse?

I want to make a symbol rotate to point at the mouse. I'm using this function, but it doesn't work below the symbol's pivot. The inverse tan function has a range of 180 degrees right? So how can i get 360 degrees of movement? Would I need to add an...

Count black pixels within a circle [closed]

I have one vector of radii and second vector of hundreds of [X,Y] coordinates. For each possible coordinate-radius pair I have count all black pixels within a circle (whose center is placed in the coordinate) on an input binary image. What is the fastest way to do it? My only...

Inverted circle radius calculation

I want to calculate the radius of an inverted circle. I managed to implement everything but, after hours of struggle, I could not find a formula to calculate the correct inverted radius. More info about circle inversion: My code so far: It seems to be working but...

Fill a 2d Array with circular area

I want an array looking like this: [ [0,0,1,1,1,0,0], [0,1,1,1,1,1,0], [1,1,1,1,1,1,1], [1,1,1,1,1,1,1], [1,1,1,1,1,1,1], [0,1,1,1,1,1,0], [0,0,1,1,1,0,0], ] My first approach was to get the circumference var steps = 100; var coord = []; var x,y; for (var i = 0; i < steps; i++) { var phase = 2 * Math.PI...

Fast way to get relative position of point and line

I have a line and point (2D). Ok, using Paul Bourke formula I can get the distance of ortogonal projection of point to the line. ( Also I can know if the solution is or not inside the segment of line. ) OK, But I'd like to know if the...

Sort list of coordinates

I have the following problem: I have a list of all the x, y coordinates of a polygon's points. I now need to sort them in such a way that I obtain the points in a clockwise order. Currently I have the 6 coordinates to draw a polygon but its...

How to scale polygon while maintaining position around circle

If I had the following figure shown below and I wanted to scale the top rectangle by some factor such that the left side will still touch the circle like it does now, how would I go about doing that? This is being done in C++ where the rectangles are...

iteratively intersecting line segments in Sympy… is there a better way?

OK. I have the points that comprise the borders of a polygon. I want to (a) use Sympy's geometry module to determine, from all of the possible line-segments between any pair of points, which segments do not cross the perimeter. This result will be the "edges" that are allowed to...

Perfect circled image turning into half an oval shape

If I have a DOM structure like this: <li> <a> <h2></h2> <img> <p></p> <button></button> </a> </li> And I want the image to be perfectly round, how would I achieve that? I tried using the following after looking at this article: .circle-box { width: 70%; height: 0; margin-top: 10px; padding-bottom: 70%;...

How can I do this more elegantly? [closed]

I have a 2D point A inside the [0,1]² square. The square is divided into 9 subsquares (of equal dimensions) I want to know which subsquare the point A belongs to. I can do a if elseif else on the first coordinate, then inside each branch, another if else...

Quaternion to EulerXYZ, how to differentiate the negative and positive quaternion

I've been trying to figure out the difference between these, and why ToEulerXYZ does not get the right rotation. Using MathGeoLib: axisX: x 0.80878228 float y -0.58810818 float z 0.00000000 float axisY: x 0.58811820 float y 0.80877501 float z 0.00000000 float axisZ: x 0.00000000 float y 0.00000000 float z 1.0000000...

Calculate The object angle(face) having two points? [closed]

C++, I want calculate the angle of the direction of the two points. Here is a picture which shows the two points and the direction of how to get the angle of the direction? p1 - start point. p2 - direction point. me need direction angle(facing?) from p1 to p2...

Generating a sphere in OpenGL without high level libraries - what's wrong with my code?

I've tried to implement an inefficient function to generate the points, normals, tex coords, and index list of a sphere. Ignoring the lines, when I draw the sphere with OpenGL I get the following output which is clearly wrong: Can anyone help me understand what's wrong with my code? public...

Rotate a grid of points in C++

If i had an array of point structs defined as struct Point{ float x; float y; }; How would I rotate the points in this array by a given angle? As an example: Any help would be appreciated!...

SQL Server Dynamic Query Issue with GEOMETRY value - 'POINT' is not a recognized built in function

I am building a stored procedure for SQL Server 2014. All works as expected except I receive an error when trying to use a geometry type to base the location results upon. USE UKStreetsAndPlaces GO DECLARE @Latitude Decimal(9,6) = 51.3083162 DECLARE @Longitude Decimal(9,6) = -0.7799193 DECLARE @LongitudeOperator AS NVARCHAR(1) DECLARE...

three.js merge geometry and octree selection

I am using Three.js to show a number of custom geometry's in various positions and rotations. They are static and rarely change, but the user can add, delete or change the shape of each individual object. This has worked well using the code snippet below: var Mat=new THREE.MeshBasicMaterial( { color:...

Geometry rounding problems: object no longer convex after simple transformations

I'm making a little app to analyze geometry. In one part of my program, I use an algorithm that has to have a convex object as input. Luckily, all my objects are initially convex, but some are just barely so (see image). After I apply some transformations, my algorithm fails...

How to efficiently test whether an arbitrary polygon intersects with a square?

The polygon may or may not be convex. We can assume that the edges of the square align with X and Y axis. Calculating the intersection [A simple algorithm for polygon intersection]1 is an over-kill, as I only need to know whether they intersect (Yes or No)....

Draw arc segments using UIBezierPath

I know there are lots of example related to UIBezierPath, but I am very confused about the angles to use to draw arc, my requirement is like the image below to draw arcs. The length and angle should be calculated as per total number. Right now I have created a...

Find a circle that touch 3 others

We have a red circle with radius 10. On this red cicle, there is 3 other circles in blue (radius 8) that are equally spaced by 120°. The point C is the middle of two blue circle and has a green circle (radius 2). I need to find the coordinate...

What is this algorithm mapping coordinates to numbers called?

I'm writing a program for visualizing crystals. As a part of the program, I have to generate all different basic points in a lattice structure. For those that aren't familiar with crystallography, you can find the most general cases of these structures here: The problem was that I wanted...

Matrix (?) to Rectangle and vise versa

I'm currently working with files that define rectangular shapes in a way that I'm unfamiliar with. Someone told me it might be a matrix, but knowing that doesn't particularly help me with my problem, converting it to points and back. For example, I have these values: 0.95, -0.28, -0.28, -0.95,...

Drawing a circle with a triangle fan

I've tried to draw a circle with Pyglet and I have failed. With beautiful and unexpected results, though. I have worked out the math: I have implemented the method: """ Pyglet utilities. Designed to ease drawing of primitives with Pyglet. """ # Dependencies import pyglet from math import sin, cos...

Check a closed cubic bezier curve path is on the inside of an ellipse

I am now scripting Illustrator by javascript. Is it possible to check a closed path is on the inside of an ellipse? (As drawn in below picture, I wish to check a red path is on the inside of an ellipse) A closed path is generally created by cubic bezier...

Trim lasso that contains closed loop

I'm plotting data in matplotlib that the user can interact with via a lasso, which is internally represented as a list of vertices that make up a polygonal chain. What I would like to be able to do is to trim lassos as illustrated in this highly professional picture: Formally,...

Oracle - Changing the value of a single ordinance in a Geometry field

I have a single Geometry Field in a table select g3e_GEOMETRY from MyTable where g3e_FID = 15463352 This returns (3001, , (, , ), (1, 1, 1, 4, 1, 0, , , , , , , , , , , , , , , , , , , , ,...

Determine if coordinate is inside region (MKMapView, solve in PHP)

I'm using MKMapView and I send my php program the visible region (center lat, center lon, span lat, span lon). I need to determine if a coordinate is inside that region using php. I'm hoping there's a standard formula somewhere, but I haven't found one. I'll keep trying to come...