Calculate Total Length of Given linear Curves ignoring Bends - math

I want to calculate the total length of the Linear (Straight curves) by Ignoring the bends.
Note: Bend angle will be 90 degrees.
I have a solution but need it in an optimal way or any formula will be welcomed.
Current Solution:
Calculate the endpoints of all Linear lines.
Intersects the line and get virtual intersection points.
Then calculate the distance between points with each linear line start and virtual intersection point.
Sum all the lengths.

Don't take it badly, but I fail to see a difficulty.
L = L1 + 2 R1 + L2 + 2 R2 + L3.
Note that the exact length (not ignoring bends) is
L = L1 + π/2 R1 + L2 + π/2 R2 + L3.

It is not quite clear what is done? Picture? Line segment and arcs description?
In any case - horizontal line will start at Y-coordinate of the previous vertical line and end at Y-coordinate of the next vertical line.
And vice versa - vertical line will start at X-coordinate of the previous horizontal line and end at X-coordinate of the next horizontal line.
So result is
Sum(Abs(X(i+2) - X(i))) + //over horizontal lines
Sum(Abs(Y(k+2) - Y(k))) + //over vertical lines
two end differences

Related

One-point perspective and point with negative depth

I am trying to create a game using one-point perspective. Everything works fine for points within the view but goes wrong with the negative depth. I understand the perspective as shown on the following picture (source).
In general, I took a point at some distance from the left of the right vertical edge of the frame along the lower horizontal line (5 points in this case), join it with the O' point (line H'O') and where the line intersects the vertical line (at point H') is the depth line (of 5 in this case). This works well even for negative depth (as the line H'O' intersect the vertical line below the viewpoint). However, if the depth is more then is the distance of O' (that mean the point would be on the right from the O') the line flip and the H' end on top of the viewpoint (although it should end up below).
How should I correct it, so the point with negative depth is transformed correctly (mean from 3D space to 2D space)?
EDIT
This image is probably better.
My question is how to handle points with negative depth (should end up below the screen) higher then is a distance of transversal.
The points to the right of the point O', along the line determined by the lower edge of the frame, correspond to points that are behind the observer, so technically, the observer cannot see them. To see the points behind you, means that you have to turn around, so you need to change the position of the screen. Draw a copy of the black square frame to the right of the point O', so that the new square is the mirror symmetric image of the original frame square with respect to the line orthogonal to the horizon line and passing trough the point O'.
Edit: The points with negative depth to the right of point O' (i.e. a point behind the observer) is supposed to be mapped above the horizontal blue line. This is the right way to go.
I assume your coordinate system in three dimensions has its origin at the lower right corner of the square frame on your picture. The x axis (I think how you measure width) runs along the lower horizontl edge of the frame, while the y axis (what you call height) is along the right vertical edge of the frame. The depth axis is in three dimensions and it's perpendicular to the plane of the square frame (so it is parallel to the ground). It starts from the lower right corner of the frame square. Assume that the distance of point O' from the right vertical edge of the square is S and the coordinates of the point C are {C1, C2} (C1 is the distance of point C from the right vertical edge and C2 is the distance of C from the lower horizontal edge of the square).
Given the coordinates {w, h, d} (w - width, h - height, d - depth) of a point in three dimensions, its representation on the two dimesnional square screen is gievn by the formulas:
x = (S*w + C1*d)/(S+d)
y = (S*h + C2*d)/(S+d)
So the points you gave as an example in the comments are
P1 = {h = 5, w = 5, d = 5} and P2 = {h = 5, w = 5, d = -10}
Their representation on the screen is
P1_screen = {(S*5 + C1*5)/(S+5), (S*5 + C2*5)/(S+5)}
P2_screen = {(S*5 - C1*10)/(S-10), (S*5 - C2*10)/(S-10)}
whatever your parameters S, C1 and C2 are. The representation of the (infinte) line connecting points P1 and P2 is represented on the screen as the (infinite) line connecting the points P1_screen and P2_screen. However, if you want the 2D representation of the visible part of the segment that connects P1 and P2, then you have to draw the (infinite) line between P1_screen and P2_screen and exclude the following two segment: segment [P1_screen, P2_screen] and the segment from P2_screen along the line up towards the upper top edge. You have to draw on the screen only the segment from the infinite line connecting P1_screen and P2_screen which starts from P1_Screen and goes down towards the lower horizontal edge of the screen.

Orientation of the Vector

I have a line and a few points and I need to determine which points are under and which are beyond the line. I tried to find a line that is in 90degrees angle with my line and crosses the points but i couldnt figure out whether the vectors orientation is up or down. Can you help? Thank you
You can find line equation and substitute points in thin equation.
Easy case: Let's line is not vertical, so it might be described by equation
y = a * x + b
for every query point (px, py) calculate value
S = py - a * px - b
When S is positive, point is above the line, when negative - below.
If your line is defined by base point B and direction vector D, you can determine - what semi-plane (against the line) query point P belongs to - using cross product sign
Sign (D x (P-B))
Note that in this case term "below" depends also on sign of X-component of vector D

2 Line segments having same end point

I had a question regarding 2 line segments. Say we have 2 line segments whose origin and lengths are given as: (P0, L0) and (P1, L1) respectively. I need to find when can they end at the same point. The line segments lie anywhere in 3D space.
One of the approaches I could think of is: Let's say this common end point is T and the points are A and B. So for the line segments with A and B as origins, A,B and T must form a triangle. Length of vector AT = L0 and length of vector BT = L1. But since the orientation of the line segment is not known, there can be a lot of possibilities. Lets say we choose a particular orientation for line segment AT as (i,j,k) - 1st octant. So now we can move anywhere in space from T but only by a distance L1 to find BT.
This is where I m not sure how to move forward.
The line segments can end in the same point if and only if the distance between P0 and P1 is less than or equal to L0 + L1. In the special case where this distance is equal to L0 + L1 the line segments have the same orientation in space and lie on the same line.
A way to think about this is to ask if two spheres around P0 and P1 with radii L0 and L1 intersect or at least touch each other. The circle (point) of intersection (touch) is where your line segments can have the same end point.

Calculate perpendicular distance from a vertical and horizontal line in

I have two lines one horizontal and one vertical, I have the equations of lines in the format:
and as it is a horizontal line and for the vertical line and I can calculate because I know a point from which the lines passes by.
I want to calculate the perpendicular distance of a point in space from each of these lines. I can't use the slop as it will give divide by 0 error. What is the other approach?
In the horizontal line the equation is y = c and so the distance of any general point (a,b) from that line is simply |b-c|.
In the vertical line, c is meaningless as the line never crosses the y axis (unless it is the y axis). The equation of such a line is x = d (you need to calculate d) then the distance of a point (a,b) from this line is simply |a-d|
EDIT: By the way this question really has nothing at all to do with C++
Beyond the fact that this problem is already solved:
For future issues like that use the normalized form
c = a*x + b*y;

Recognising the type of graph by given set of points

I'm trying to design a program that draws graphs given a set of points (x, y), and it also should recognize the curve (straight line, hyperbole, parabola), with only the help of the points.
Is there an algorithm to do that?
You'll need five points if the curve could be a straight line or a conic (hyperbola, parabola, ellipse, circle).
If the five points are collinear, you have a straight line. (Or a degenerate conic? But if you're expecting straight lines, this should indicate a straight line.)
If four are collinear, you have a degenerate conic, given by the line through the four collinear points and any line through the fifth point that is not parallel to the first line.
If three are collinear, you have a degenerate conic, given by the line through the three collinear points and the line through the two other points. (Unless these two lines are parallel, in which case this isn't a conic.)
If no three points are collinear, you have a unique, non-degenerate conic.
To find the equation for this conic (Ax^2 + Bxy + Cy^2 + Dx + Ey + F = 0), take a look at this page, specifically the formula in the DETAILS section. Put in your five x and y values, calculate the determinant of the matrix in terms of x and y, and this will give you the formula of your conic. Then see here to figure out what kind of conic you have based on the values of A, B and C.
If you have more than 5 points, pick five points (preferably so no three are collinear), find the conic, and then check that the remaining points lie on the conic.
You can do it by watching the function extreme but is maybe not optimal solution for this problem (i mean a problem in parabola function like that y = sqrt(x*x-1)).
For straight line you can calc y = ax + b by the 2 random point's and check what other points equal this condition, if yes. This is straight line if no you may check a 2 other exceptions or nothing from it. Maybe somone else get solution for next 2 cases?

Resources