I'm trying to generate SVG paths that repeat. To do this I am plotting a minimum of 2 points on a graph to create a line like so...
However I want to draw a straight line from the last point to the next point of the repeated path on the next SVG, and to do that I need to find the Y position where the current graph ends and the next starts.
Based on the x and y coordinates of the 2 points, how do I calculate the y coordinate (that's in red and should be 40 in this example)
Any help greatly appreciated!
Assuming the tile size is a x a, you can construct a coordination system from the bottom left of the first tile. Also assume the finish of the first line is (x1,y1) and in this new coordination system, the start of the second line would be (a+x2,y2). Look at the following paint masterpiece (:D) to understand what I mean.
OK, now you can write the line's formula of these two points:
y-y1=((y2-y1)/(a+x2-x1))(x-x1)
replacing x with a and solving the formula for y would result in what you need:
y=((y2-y1)/(a+x2-x1))(a-x1)+y1
UPDATE
From the values you represented, you can use the formula by replacing x1=70, x2=20, y1=100, y2=0, a=100
Related
I need to implement a chart to represent data on a simple X,Y axis, X being time.
Think of e.g. speedtest or RAM usage over time.
When I start drawing, I have no data. As time passes by, more data comes to my set and I can use it to draw chart. Given that I have 1 [X,Y] coordinate for every second, I want to draw this as a continuous line.
Obviously, Bezier curves came to my mind, but the problem is that my points are always changing. At one second I have N points, in the next one N+1. And if I decide to work only with the last M points, this will not be a continuous line. Every time I add a new last point P to my Bezier curve, it changes the curve as a whole, not just the part between P and P-1.
So what is the correct approach to this problem? Is Bezier curve a viable solution with some hack, or do I have to use some other approach?
Thanks!
If there is a point that is given in x,y co-ordinate, we can pass infinite number of straight lines from it by varying slope a and intercept b.
But when we fix our x and y and vary our a and b in a,b co-ordinate we get a single unique line. Why?
I found this annoying while studying Hough transform.
I don't fully understand what you want to ask....but yes this may clear things a bit. When you fix a point you can make infinite lines pass through it.This is obvious. Now you fix the a,b as co-ordinates that is you have fixed for one particular case the slope and the y-intercept both.
Now note that the slope can vary between a definite range. If you consider the argument, it will vary from 0 degrees to 180 degrees. now out of the infinite lines having different slopes that pass through a point (in this case the point on the y-axis i.e. the y-intercept of your line) you are essentially selecting one line in particular having a specific slope. now only one such line is possible.
I hope this answers your question...
Please see below image. This image is created by first converting a two-column data frame into a study window (call it study_win) using as.owin, and then plotting another two-columns data-frame (call it study_points)on top of the window.
It is clear that the points are lying inside the window! However when I call
ppp(study_points[,1],study_points[,2],win = study_window)
it says that most of my points are rejected as lying outside the window. Could someone tell me what is going on?
Thanks!
First you could have taken a step back to check that the window object study_window was what you intended. You could have plotted or printed this object in its own right. A plot of study_window would show (and you can also see this in the plot that you supplied in the question) that the boundary of the window is a disconnected scatter of points, not a joined-up polygon. A printout of study_window would have revealed that it is a binary pixel mask, with a very small area, rather than a polygonal region. The help for as.owin explains that, when as.owin is applied to a dataframe containing columns of x,y coordinates, it interprets them as pixel coordinates of the pixels that lie inside the window.
So,what has happened is that as.owin has created a window consisting of one pixel at each of the (x,y) locations in the data frame. That's not what you wanted; the (x,y) coordinates were meant to be the vertices of a polygonal boundary.
To get the desired window, do something like study_window <- owin(poly=df) where df is the data frame of (x,y) coordinates of vertices.
To do it all in one step, type something like mypattern <- ppp(x, y, poly=df) where x and y are the vectors of coordinates of the points in the window.
so I solved the problem by using the "owin" and specify the region to be polygon; instead of "as.owin". I have no idea the difference between owin and as.owin, but I am just glad it worked...
I am trying to do flight planning in 3D space, but I actually want to figure out doing it in 2D space first. I have:
an object with a known current position and known current velocity vector
a desired point in space
a desired velocity vector
a desired time
I want to plan a route for the object to take to reach the desired point at the desired time travelling at the desired vector, and taking into account the objects starting vector.
I'm at a bit of a loss on how to achieve this. Any help appreciated.
As noted by eigenchris in a comment, this problem has many possible solutions, so I will propose a method that gives sensible results and allows for flexible customization, depending on the desired properties of your path.
It's easier to split the problem into two parts, first find a path in 2D and then compute the distance along it and solve the problem of matching the velocity and acceleration to the distance as a 1D (plus time) problem.
For the path, Cubic Bézier curves seem ideally suited to the problem. A cubic Bézier curve is defined by 4 control points P0, P1, P2 and P3.
P0 and P3 are the start and end points of the curve. P1 and P2 are control points that are used for specifying the tangent of the curve at the start and end points (defined by the lines P0-P1 and P2-P3 respectively). If you fly along the curve, those are the directions you are moving at the start and end points.
Here is an interactive demo to get a feel for how the positioning of the control points influences the curve (the cubic Bézier is the blue curve on the right with 4 control points).
To define a flight path using a Bézier curve:
Set P0 to the start point of your flight path
Set P3 to the end point
Set P1 to a point in the direction of the starting velocity vector from P0
Set P2 to a point in the opposite direction to the ending velocity vector from P3
Note that the distances from P0 to P1 and from P2 to P3 do not represent the magnitude of the starting and ending velocities. Rather, they specify the tightness of the turn at the start and end of the curve to align the curve with the start and end tangents. Pull the control points in close for a tight turn, push them further out for a wider turn. However if you want to you can make the turn wider the larger the desired velocity vector is, to be more physically realistic.
If you don't want to be turning the whole time, you can segment the path into several Bézier curves, or a Bézier curve then a straight line then a Bézier curve, and make the tangents match up where the different segments meet. For example you might want to take a curved path at the start of the flight path, then follow a straight line for most of the way, the follow a curved path at the end to line up with the desired final direction vector. This gives you total control over your flight path.
The solution generalizes easily to 3 dimensions.
Now that you have a path, you need to figure out how to accelerate and decelerate along the curve to arrive at the destination at the right time and the right speed. First, calculate the distance along the curve:
Arc Length of Bézier Curves
If you know the start time, the end time and the start and end speeds (magnitude of start and end velocity vectors), you can work out how far along the curve you would have flown between the start and end times, assuming you linearly accelerate from the starting speed to the ending speed over the course of the journey (distance is the area under the line):
Since this area will most likely be different to the computed distance along the Bézier Curve, you need to create a segmented function that has the desired area under the line between the start and end times. The image shows two such functions to handle the cases where the distance traveled is greater than desired, and the area is decreased by slowing down then travelling at constant speed then speeding up, and vice versa. The examples show instantaneous changes in acceleration, but not in speed. You can choose any function you like as long as the start and end speeds at the start and end times match the desired values and the area under the function is equal to the distance along the path.
Given two convex polygons in 2D space, how would you go about constructing the line segment(s ) which, at any point on the lines, is equidistant from the closest point of either convex polygon?
I'm looking towards an implementation of Voronoi diagrams for convex polygons instead of points, but I'm unsure how to even begin calculating the line for just two polygons. So I figured I'd take this one step at a time and start here.
Edit To try to make the question a little clearer, I want to bisect the plane (or a subset thereof).
Suppose we have polygon A on the left and polygon B on the right. There will be some line of bisection that divides the plane into points on the left and points on the right. Every point on the line is equally distance from either polygon. Every point left of the line is closer to polygon A than to polygon B. Every point right of the line is closest to polygon B.
Here's an image generated by a Matlab script I wrote that brute-forces an approximation:
The problem, I believe, is not as simple as examining the space in "between" the two polygons, since the line must extend beyond the area directly between the two shapes. And ideally I'd like to find a solution that generalizes to more than two shapes, which, to me, seems to complicate the problem a great deal more. Here's a (obviously very rough) approximation of how that might look:
Well, proceeding one step at a time I'd look at the closest points in the polygons themselves. Let's say a in A is the closest point to B and b in B is the closest point to A. You know the middle point of AB is in the desired segments.
What are the posibilities for a? It can be a vertex of A or it can be a point in one side. The same applies for b. What happens with the "equidistant-segments"? How to build them in each case?
Since those segments are equidistant to sides of the polygons, they have to be part of the line that bissects the angle of the lines containing the corresponding sides.
Am I understanding you correctly but assuming you're wanting the line that effectively bisects the space between 2 convex polygons? If so, then ...
find the line that joins the 2 polygons (P1 & P2)
find each polygon centre (P1.centre & P2.centre) by calculating the average X and Y coordinate.
find the vertex on each polygon that's closest to the other's centre (P1.vc & P2.vc)
given that P1.vc & P2.vc now define the line joining P1 & P2
find the midpoint (mp) of P1.vc & P2.vc
Bisecting line = perpendicular of the line joining P1.vc & P2.vc that passes through mp