Libgdx polygon triangulation - libgdx

Ok, so I have a polygon (simple but concave) that I'm trying to cut into triangles to make it collide with an other polygon.
I knew my polygone was concave, so i decided to use LibGDX EarClippingTriangulator to manage to cut it into triangles.
So, with this code, I get my triangles vertices :
public void triangulate()
{
Vector<float[]> trianglesVertices = new Vector<float[]>();
ShortArray pointsCoords = new ShortArray();
EarClippingTriangulator triangulator = new EarClippingTriangulator();
// Cut in triangles
pointsCoords = triangulator.computeTriangles(this.getTransformedVertices());
// Make triangles
for (int i = 0; i < pointsCoords.size / 6; i++)
{
trianglesVertices.add(new float[] {
pointsCoords.get(i), pointsCoords.get(i+1),
pointsCoords.get(i+2), pointsCoords.get(i+3),
pointsCoords.get(i+4), pointsCoords.get(i+5),
});
Polygon triangle = new Polygon(trianglesVertices.get(i));
triangles.add(triangle);
}
System.out.printf("Triangulation made %d triangles.\n", pointsCoords.size / 6);
}
But when i try to draw thoses triangles I just made,
they just stack in the 0,0 coord..
And, is it normal that all triangles seems almost the sames, I mean they all got the same orientation ?
I didn't found so much info about this trangulation use for libgdx
Can you help ?
(Sorry for my english i'm french, and sorry for no pictures, i'm too young here)
EDIT: This is my polygon (in CCW)
hitbox.setVertices(new float[]{
this.getX() + 13, this.getY() - 60,
this.getX() + 16, this.getY() - 74,
this.getX() + 39, this.getY() - 74,
this.getX() + 45, this.getY() - 105,
this.getX() + 81, this.getY() - 105,
this.getX() + 88, this.getY() - 74,
this.getX() + 108, this.getY() - 74,
this.getX() + 114, this.getY() - 61,
this.getX() + 106, this.getY() - 30, // Top right
this.getX() + 101, this.getY() - 29,
this.getX() + 101, this.getY() - 57,
this.getX() + 83, this.getY() - 62,
this.getX() + 75, this.getY() - 50,
this.getX() + 65, this.getY() - 4, // Top mid
this.getX() + 62, this.getY() - 4, // Top mid
this.getX() + 52, this.getY() - 50,
this.getX() + 44, this.getY() - 62,
this.getX() + 25, this.getY() - 56,
this.getX() + 25, this.getY() - 30,
this.getX() + 19, this.getY() - 30, // Top left
});
EDIT2: Now i got enough point to show you the polygon here it is

The issue here is that EarClippingTriangulator.computeTriangles is returning different output than you're expecting. It returns an array of indices, where each index represents a vertex in the array you passed in originally. So if you passed in an array of size 8, that would represent 4 vertices; the array returned would be size 6 (3 vertices for triangle 1, 3 vertices for triangle 2).
Let's say you have a 2 x 3 rectangle with the bottom left at (5, 5). Your vertices would be:
Vertex 0: (5, 5)
Vertex 1: (7, 5)
Vertex 2: (7, 10)
Vertex 3: (5, 10)
You would flatten those down to an array and pass them into computeTriangles as:
[5, 5, 7, 5, 7, 10, 5, 10]
The returned array would look something like this:
[0, 3, 2, 2, 1, 0]
Each set of three indices in the returned array forms a triangle. Each of those values represents the index of a vertex in the data you passed in. In this example, the output would be telling you to use these vertices:
Triangle 1: Vertex 0, Vertex 3, Vertex 2
Triangle 2: Vertex 2, Vertex 1, Vertex 0
So you would need to draw these triangles:
Triangle 1: (5, 5) -> (5, 10) -> (7, 10)
Triangle 2: (7, 10) -> (7, 5) -> (5, 5)
Note that this is not necessarily the actual output you would get with the inputs I provided, it's just a sample designed to illustrate how you're supposed to use the data returned.
Here's the code I ended up with, which will correctly draw Polygon instances, although you can do the same thing with any properly formulated vertex array:
private void drawFilledPolygon(Polygon polygon, Color color) {
shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
shapeRenderer.setColor(color);
float[] vertices = polygon.getTransformedVertices();
// NOTE: you probably don't want to create a new EarClippingTriangulator each frame
ShortArray triangleIndices = new EarClippingTriangulator().computeTriangles(vertices);
for (int i = 0; i < triangleIndices.size; i += 3) {
shapeRenderer.triangle(
vertices[triangleIndices.get(i) * 2], vertices[triangleIndices.get(i) * 2 + 1],
vertices[triangleIndices.get(i + 1) * 2], vertices[triangleIndices.get(i + 1) * 2 + 1],
vertices[triangleIndices.get(i + 2) * 2], vertices[triangleIndices.get(i + 2) * 2 + 1]
);
}
shapeRenderer.end();
}

The problem is with your loop:
// Make triangles
for (int i = 0; i < pointsCoords.size / 6; i++)
{
trianglesVertices.add(new float[] {
pointsCoords.get(i), pointsCoords.get(i+1),
pointsCoords.get(i+2), pointsCoords.get(i+3),
pointsCoords.get(i+4), pointsCoords.get(i+5),
});
Polygon triangle = new Polygon(trianglesVertices.get(i));
triangles.add(triangle);
}
First triangle will have correct coordinates, but second one will will use 1, 2, 3, 4, 5, 6 elements of pointsCoords that doesn't make any sence. You should multiply i by 6 inside loop to take offset into account:
pointsCoords.get(i*6), pointsCoords.get(i*6 + 1),
pointsCoords.get(i*6 + 2), pointsCoords.get(i*6 + 3),
pointsCoords.get(i*6 + 4), pointsCoords.get(i*6 + 5),

Related

Defining Octave Functions

I am working on a school project with Octave for calculating and plotting velocity/acceleration graphs.
I have been trying to create a subplot function so that I won't have to hardcore it for every subplot as such
subplot(3, 1, 1);
plot(time, accn);
grid;
title('Acceleration vs Time')
xlabel('Time, (s)')
ylabel('Acceleration, (m/s^2)')
subplot(3, 1, 2);
plot(time, velocity);
grid;
title('Velocity vs Time');
xlabel('Time, (s)');
ylabel('Velocity, (m/s)');
Is it possible to create a function akin to this
subplot = subplotFunction(row, column, xaxis, yaxis, header, xaxisLabel,
yaxisLabel)
subplot(3, row, column);
plot(xaxis, yaxis);
grid;
title('header')
xlabel('xaxisLabel')
ylabel('yaxisLabel')
endfunction
And then call it like this?
subplot = subplotFunction(1, 1, time, accn, 'Acceleration vs Time', 'Time, (s)', 'Acceleration, (m/s^2)')
I am quite new to using functions so my apologies :(
1;
function subplotFunction(row, column, idx, xaxis, yaxis, header, xaxisLabel, yaxisLabel)
subplot (row, column, idx);
plot (xaxis, yaxis);
grid on;
title (header)
xlabel (xaxisLabel)
ylabel (yaxisLabel)
endfunction
subplotFunction (3, 1, 1, 1:10, 11:20, "foo", "bar", "baz")
subplotFunction (3, 1, 2, 1:10, 11:20, "huhu", "haha", "hoho")
x = linspace (0, 10, 100);
subplotFunction (3, 1, 3, x, sin(x), "world", "boo", "doo")
print ("out.png")
gives

Curve Fitting past last data point(s)

I am trying to fit a curve to a set of data points but would like to preserve certain characteristics.
Like in this graph I have curves that almost end up being linear and some of them are not. I need a functional form to interpolate between the given data points or past the last given point.
The curves have been created using a simple regression
def func(x, d, b, c):
return c + b * np.sqrt(x) + d * x
My question now is what is the best approach to ensure a positive slope past the last data point(s) ??? In my application a decrease in costs while increasing the volume doesn't make sense even if the data says so.
I would like to keep the order as low as possible maybe ˆ3 would still be fine.
The data used to create the curve with the negative slope is
x_data = [ 100, 560, 791, 1117, 1576, 2225,
3141, 4434, 6258, 8834, 12470, 17603,
24848, 35075, 49511, 69889, 98654, 139258,
196573, 277479, 391684, 552893, 780453, 1101672,
1555099, 2195148, 3098628, 4373963, 6174201, 8715381,
12302462, 17365915]
y_data = [ 7, 8, 9, 10, 11, 12, 14, 16, 21, 27, 32, 30, 31,
38, 49, 65, 86, 108, 130, 156, 183, 211, 240, 272, 307, 346,
389, 436, 490, 549, 473, 536]
And for the positive one
x_data = [ 100, 653, 950, 1383, 2013, 2930,
4265, 6207, 9034, 13148, 19136, 27851,
40535, 58996, 85865, 124969, 181884, 264718,
385277, 560741, 816117, 1187796, 1728748, 2516062,
3661939, 5329675, 7756940, 11289641, 16431220, 23914400,
34805603, 50656927]
y_data = [ 6, 6, 7, 7, 8, 8, 9, 10, 11, 12, 14, 16, 18,
21, 25, 29, 35, 42, 50, 60, 72, 87, 105, 128, 156, 190,
232, 284, 347, 426, 522, 640]
The curve fitting is simple done by using
popt, pcov = curve_fit(func, x_data, y_data)
For the plot
plt.plot(xdata, func(xdata, *popt), 'g--', label='fit: a=%5.3f, b=%5.3f, c=%5.3f' % tuple(popt))
plt.plot(x_data, y_data, 'ro')
plt.xlabel('Volume')
plt.ylabel('Costs')
plt.show()
A simple solution might just look like this:
import matplotlib.pyplot as plt
import numpy as np
from scipy.optimize import least_squares
def fit_function(x, a, b, c, d):
return a**2 + b**2 * x + c**2 * abs(x)**d
def residuals( params, xData, yData):
diff = [ fit_function(x, *params ) - y for x, y in zip( xData, yData ) ]
return diff
fit1 = least_squares( residuals, [ .1, .1, .1, .5 ], loss='soft_l1', args=( x1Data, y1Data ) )
print fit1.x
fit2 = least_squares( residuals, [ .1, .1, .1, .5 ], loss='soft_l1', args=( x2Data, y2Data ) )
print fit2.x
testX1 = np.linspace(0, 1.1 * max( x1Data ), 100 )
testX2 = np.linspace(0, 1.1 * max( x2Data ), 100 )
testY1 = [ fit_function( x, *( fit1.x ) ) for x in testX1 ]
testY2 = [ fit_function( x, *( fit2.x ) ) for x in testX2 ]
fig = plt.figure()
ax = fig.add_subplot( 1, 1, 1 )
ax.scatter( x1Data, y1Data )
ax.scatter( x2Data, y2Data )
ax.plot( testX1, testY1 )
ax.plot( testX2, testY2 )
plt.show()
providing
>>[ 1.00232004e-01 -1.10838455e-04 2.50434266e-01 5.73214256e-01]
>>[ 1.00104293e-01 -2.57749592e-05 1.83726191e-01 5.55926678e-01]
and
It just takes the parameters as squares, therefore ensuring positive slope. Naturally, the fit becomes worse if following the decreasing points at the end of data set 1 is forbidden. Concerning this I'd say those are just statistical outliers. Therefore, I used least_squares, which can deal with this with a soft loss. See this doc for details. Depending on how the real data set is, I'd think about removing them. Finally, I'd expect that zero volume produces zero costs, so the constant term in the fit function doesn't seem to make sense.
So if the function is only of type a**2 * x + b**2 * sqrt(x) it look like:
where the green graph is the result of leastsq, i.e. without the f_scale option of least_squares.

out of stack space (infinite loop?) error while working with matplolib and Django

I am trying to show a graph which I am trying to plot in Matplotlib and then showing it with some hard coding in HTML template.But out of 3 attempts, it is working only once else it is throwing TCL Out of stack space error. Below is my coding.
def similar_images(request):
n_groups = 5
means_men = (20, 35, 30, 35, 27)
std_men = (2, 3, 4, 1, 2)
means_women = (25, 32, 34, 20, 25)
std_women = (3, 5, 2, 3, 3)
fig, ax = plt.subplots() # somwhere here it is throwing this error
index = np.arange(n_groups)
bar_width = 0.35
opacity = 0.4
error_config = {'ecolor': '0.3'}
rects1 = plt.bar(index, means_men, bar_width,
alpha=opacity,
color='b',
yerr=std_men,
error_kw=error_config,
label='Men')
rects2 = plt.bar(index + bar_width, means_women, bar_width,
alpha=opacity,
color='r',
yerr=std_women,
error_kw=error_config,
label='Women')
plt.xlabel('Shoe')
plt.ylabel('Week')
plt.title('Last Year sale details')
#plt.xticks(index + bar_width / 2, ('A', 'B', 'C', 'D', 'E'))
plt.legend()
plt.savefig('/graph.png')
plt.close()
return render(request,'sales/Details.html')
Please, can someone help me here?
Below is the error.
Exception Type: TclError at /sales/similar_images/
Exception Value: out of stack space (infinite loop?)

Single function to bubble sort 2 lists

Good evening. I have managed to bubble sort listOne. ListTwo will also need sorting. Is there a way to add listTwo into the bubble sort that I already have so that it gets sorted as well.
Or do I need to write another loop?
listOne = [3, 9, 2, 6, 1]
listTwo = [4, 8, 5, 7, 0]
def bubbleSort (inList):
moreSwaps = True
while (moreSwaps):
moreSwaps = False
for element in range(len(listOne)-1):
if listOne[element]> listOne[element+1]:
moreSwaps = True
temp = listOne[element]
listOne[element]=listOne[element+1]
listOne[element+1]= temp
return (inList)
print ("List One = ", listOne)
print ("List One Sorted = ", bubbleSort (listOne))
print ("List Two = ", listTwo)
print ("List Two Sorted = ", bubbleSort (listTwo))
I think you just need one method and then call the call it on the two list you can try this:
That is one method to do two jobs for you.
listOne = [3, 9, 2, 6, 1]
listTwo = [4, 8, 5, 7, 0]
def bubblesort(array):
for i in range(len(array)):
for j in range(len(array) - 1):
if array[j] > array[j + 1]:
swap = array[j]
array[j] = array[j + 1]
array[j + 1] = swap
print(array)
bubblesort(listOne)
bubblesort(listTwo)
[1, 2, 3, 6, 9]
[0, 4, 5, 7, 8]

pm3d in gnuplot with binary data

I have some data files with content
a1 b1 c1 d1
a1 b2 c2 d2
...
[blank line]
a2 b1 c1 d1
a2 b2 c2 d2
...
I plot this with gnuplot using
splot 'file' u 1:2:3:4 w pm3d.
Now, I want to use a binary file. I created the file with Fortran using unformatted stream-access (direct or sequential access did not work directly). By using gnuplot with
splot 'file' binary format='%float%float%float%float' u 1:2:3
I get a normal 3D-plot. However, the pm3d-command does not work as I don't have the blank lines in the binary file. I get the error message:
>splot 'file' binary format='%float%float%float%float' u 1:2:3:4 w pm3d
Warning: Single isoline (scan) is not enough for a pm3d plot.
Hint: Missing blank lines in the data file? See 'help pm3d' and FAQ.
According to the demo script in http://gnuplot.sourceforge.net/demo/image2.html, I have to specify the record length (which I still don't understand right). However, using this script from the demo page and the command with pm3d obtains the same error message:
splot 'scatter2.bin' binary record=30:30:29:26 u 1:2:3 w pm3d
So how is it possible to plot this four dimensional data from a binary file correctly?
Edit: Thanks, mgilson. Now it works fine. Just for the record: My fortran code-snippet:
open(unit=83,file=fname,action='write',status='replace',access='stream',form='unformatted')
a= 0.d0
b= 0.d0
do i=1,200
do j=1,100
write(83)real(a),real(b),c(i,j),d(i,j)
b = b + db
end do
a = a + da
b = 0.d0
end do
close(83)
The gnuplot commands:
set pm3d map
set contour
set cntrparam levels 20
set cntrparam bspline
unset clabel
splot 'fname' binary record=(100,-1) format='%float' u 1:2:3:4 t 'd as pm3d-projection, c as contour'
Great question, and thanks for posting it. This is a corner of gnuplot I hadn't spent much time with before. First, I need to generate a little test data -- I used python, but you could use fortran just as easily:
Note that my input array (b) is just a 10x10 array. The first two "columns" in the datafile are just the index (i,j), but you could use anything.
>>> import numpy as np
>>> a = np.arange(10)
>>> b = a[None,:]+a[:,None]
>>> b
array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
[ 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
[ 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
[ 4, 5, 6, 7, 8, 9, 10, 11, 12, 13],
[ 5, 6, 7, 8, 9, 10, 11, 12, 13, 14],
[ 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
[ 7, 8, 9, 10, 11, 12, 13, 14, 15, 16],
[ 8, 9, 10, 11, 12, 13, 14, 15, 16, 17],
[ 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]])
>>> with open('foo.dat','wb') as foo:
... for (i,j),dat in np.ndenumerate(b):
... s = struct.pack('4f',i,j,dat,dat)
... foo.write(s)
...
So here I just write 4-floating point values to the file for each data-point. Again, this is what you've already done using fortran. Now for plotting it:
splot 'foo.dat' binary record=(10,-1) format='%float' u 1:2:3:4 w pm3d
I believe that this specifies that each "scan" is a "record". Since I know that each scan will be 10 floats long, that becomes the first index in the record list. The -1 indicates that gnuplot should keep reading records until it finds the end of the file.