Efficiently drawing lines using python - python

I am trying to work out an efficient way of drawing line patterns on a screen using python.
A while ago I cobbled together some code that could drive a LAser Spirograph. Which is basically 3 circles each rotating around a moving point of the circumference of each other. This produces a series of point that trace out various patterns depending on the relative speeds each of the circles are spinning.
So now i have a long list of x,y cord that I want to plot as a single line, is there a function in python (currently playing with Pygames) that will take a series of points and conect them with a smooth curved line.
for example say i have a few hundred point that describe the pattern in this link, is there a "simple" way in Python to render he full pattern to screen. I am assuming this would be faster than trying to plot each of several thousand points to get the appearance of a line.
EDIT
I have been assuming that there would be a faster way to draw say a semi circle to the screen by passing a function the start and end point plus the radius. rather than calculate 2,000 points on the curve and plotting them separately either as points or short straight lines. I also am assuming its quicker to plot a straight line 100 points long than outputting the same line as separate points?

Disclaimer This is not an answer
To expand on my comment above
You don't want the curve. You want to sample the curve with a frequency sufficient to fool perception
have a look at the effects of denser vs coarser sampling on the perception of what is drawn on a screen
import numpy as np
import matplotlib.pyplot as plt
t = np.linspace(0, np.pi*3, 301)
x = np.sin(t)
each of the points (t[i], x[i]) represent a point, here a pont in a sine curve, in your application a more complex curve, so let's plot these points using a different sampling step, to see if the sampling rate has an effect on your perception of what is drawn...
for step in (1, 25, 50):
plt.plot(t[::step], x[::step], label='step=%d'%step)
# ^^^^ ^^^^
plt.ylim((-1.05, 1.05))
plt.legend(loc='best')
that gives me
I dare say, with a sufficiently high sampling rate what is drawn looks like a curve, even if it is a sequence of straight segments (as it's apparent when you look at the other two istances of t vs x).
In other comments, you were told which pygames function you can use to do your deed.

Related

How to detect fluctuations in point cloud height

I intercepted the point cloud in one direction and represented it within two-dimensional coordinates. As shown in the figure, it is the curve presented by the z-axis of the point cloud (y-axis in the figure) and the y-axis (x-axis in the figure).
I would like to detect fluctuations in the z-axis range, but I don't currently have a viable method in mind. Because, the number of peaks and valleys in it are uncertain and the fluctuations are also uncertain. I would like to plot my heights like this.
I tried to use clustering, but it was difficult. Is there a good way to do this please? Thanks!

Interpolating Specific Point Using Centripedal Catmull-Rom Spline

I have been investigating Catmull-Rom (CR) splines for interpolating curves and like my results so far, though I am having a fatal issue: Essentially, I am unable to map the "t" (say 0<=t<=1 where it measures the... for lack of a better way to explain it... 'parameterized' horizontal distance from P1 to P2 (using the P0,P1,P2,P3) notation here) to a tangible "x" value that would actually be charted in an X,Y coordinate space. Of course this is mostly a problem when using non-uniform CR splines (because xi=(ti-t0)+x0 in that case). I specifically like the features of the centripedal (alpha=0.5) CR splines, but the best I can do right now is ask my function to give me back a load of points and draw the curve. I am unable to say for example "Give me the point on the curve when x=some point i want. I have been able to recreate CR splines in excel and python, but still having no luck with the t->x mapping and my ultimate goal, asking to interp a specific x point and returning the corresponding y-value.
https://en.wikipedia.org/wiki/Centripetal_Catmull%E2%80%93Rom_spline is a good summary of what I have (the python code there is close to mine as well)

Pyplot - Drawing a log-log heat map under a curve

I've been struggling with this problem for days, but haven't yet found an answer on the site, so here goes!
I've made a simple straight line plot made in python, using matplotlib.pyplot - It's essentially a triangle, bounded by two straight lines and the y-axis, with a log-log scale. (I can upload the plot if this isn't a clear description, but I've not enough reputation to do so in this post - Sorry!)
The difficult part is, I now need to fill that triangle (and only that area ideally) with a heat map to show the values of a 3rd parameter, which depends on x and y in an extremely complex way. There's no simple function to describe z(x,y), but I have numerical tables giving the values of z at a range of discrete x and y values.
Is it at all possible to create such a graph (especially bearing in mind that the basic plot also has logarithmic axes)?
Thanks for reading!

How interpolate 3D coordinates

I have data points in x,y,z format. They form a point cloud of a closed manifold. How can I interpolate them using R-Project or Python? (Like polynomial splines)
It depends on what the points originally represented. Just having an array of points is generally not enough to derive the original manifold from. You need to know which points go together.
The most common low-level boundary representation ("brep") is a bunch of triangles. This is e.g. what OpenGL and Directx get as input. I've written a Python software that can convert triangular meshes in STL format to e.g. a PDF image. Maybe you can adapt that to for your purpose. Interpolating a triangle is usually not necessary, but rather trivail to do. Create three new points each halfway between two original point. These three points form an inner triangle, and the rest of the surface forms three triangles. So with this you have transformed one triangle into four triangles.
If the points are control points for spline surface patches (like NURBS, or Bézier surfaces), you have to know which points together form a patch. Since these are parametric surfaces, once you know the control points, all the points on the surface can be determined. Below is the function for a Bézier surface. The parameters u and v are the the parametric coordinates of the surface. They run from 0 to 1 along two adjecent edges of the patch. The control points are k_ij.
The B functions are weight functions for each control point;
Suppose you want to approximate a Bézier surface by a grid of 10x10 points. To do that you have to evaluate the function p for u and v running from 0 to 1 in 10 steps (generating the steps is easily done with numpy.linspace).
For each (u,v) pair, p returns a 3D point.
If you want to visualise these points, you could use mplot3d from matplotlib.
By "compact manifold" do you mean a lower dimensional function like a trajectory or a surface that is embedded in 3d? You have several alternatives for the surface-problem in R depending on how "parametric" or "non-parametric" you want to be. Regression splines of various sorts could be applied within the framework of estimating mean f(x,y) and if these values were "tightly" spaced you may get a relatively accurate and simple summary estimate. There are several non-parametric methods such as found in packages 'locfit', 'akima' and 'mgcv'. (I'm not really sure how I would go about statistically estimating a 1-d manifold in 3-space.)
Edit: But if I did want to see a 3D distribution and get an idea of whether is was a parametric curve or trajectory, I would reach for package:rgl and just plot it in a rotatable 3D frame.
If you are instead trying to form the convex hull (for which the word interpolate is probably the wrong choice), then I know there are 2-d solutions and suspect that searching would find 3-d solutions as well. Constructing the right search strategy will depend on specifics whose absence the 2 comments so far reflects. I'm speculating that attempting to model lower and higher order statistics like the 1st and 99th percentile as a function of (x,y) could be attempted if you wanted to use a regression effort to create boundaries. There is a quantile regression package, 'rq' by Roger Koenker that is well supported.

How to use interpolation to calculate a force based on angle

I am trying to make a python script that will output a force based on a measured angle. The inputs are time, the curve and the angle, but I am having trouble using interpolation to fit the force to the curve. I looked at scipy.interpolate, but I'm not sure it will help me because the points aren't evenly spaced.
numpy.interp does not require your points to be evenly distributed. I'm not certain if you mean by "The inputs are time, the curve and the angle" that you have three independent variables, if so you will have to adapt it quite a bit... But for one-variable problems, interp is the way to go.

Categories