# Has somebody an ellipse mesh code ?

Hello all,
Has somebody an ellipse mesh code non textured and without image or shader, I mean only done with vertices ? Thanks

I had none, but i just wrote this one for you:

``````
-- ellipse mesh

function setup()
ms = mesh()
local x,y,i,angle
local a,b = 100,200
local vertices = {}
for i = 1,360 do
table.insert(vertices,vec3(0,0,0))
x = a*cos(angle)
y = b*sin(angle)
table.insert(vertices,vec3(x,y,0))
x = a*cos(angle)
y = b*sin(angle)
table.insert(vertices,vec3(x,y,0))
end
ms.vertices = vertices
ms:setColors(77, 255, 0, 255)
end

function draw()
-- This sets a dark background color
background(40, 40, 50)
translate(WIDTH/2,HEIGHT/2)
rotate(30*ElapsedTime)
ms:draw()

end
``````

Yes.

``````    local fs = function(i)
return (math.sin(i) + 1) * 0.5
end
local fc = function(i)
return (math.cos(i) + 1) * 0.5
end
w, h = spriteSize(img)
m = mesh()
local v = {}
local tc = {}
local c = {}
local ic = 255 -- Inside brightness
local oc = 255 -- Outside brightness
local ia = 255 -- Inside alpha
local oa = 255 -- Outside alpha
local precision = 32
for rad = 0, (2 * math.pi) - (math.pi / precision), math.pi / precision do
table.insert(v, vec2(w / 2, h / 2))
table.insert(tc, vec2(0.5, 0.5))
table.insert(c, color(oc, oa))
table.insert(c, color(ic, ia))
table.insert(c, color(oc, oa))
end
m.vertices = v
m.texCoords = tc
m.colors = c
m.texture = img
``````

To use it without an image just comment out img = readImage(“Cargo Bot:Codea Icon”), replace local w, h = spriteSize(img) with local w, h = (specify width), (specify height), and comment out m.texCoords = tc and m.texture = img.

You can change “precision” to whatever you want, but be careful because sometimes there’s a crack at the top.

Thanks you two. You are very kind to share your code so easily and so quick. Thank you really I ll watch this. Have you hard coded this meshes or did you obtain it from any software or app ?

To Jmv38 thank you once again cause I ll do something with your code anyway. But I was unclear. I spoke about a hollow ellipse, not a filled one and more precisely a circle mesh code, not a disk one. Thanks

Hollow ellipse. Personal code. Free to use

``````
-- ellipse mesh
function setup()
local largeAxis,smallAxis,border = 200,100,10
ms = meshEllipse(largeAxis,smallAxis,border)
end

function meshEllipse(largeAxis,smallAxis,border)
ms = mesh()
local vertices = {}

local function vert(a,b,c,i)
local x = (a-c)*cos(angle)
local y = (b-c)*sin(angle)
table.insert(vertices,vec3(x,y,0))
end

local x,y,angle
local a,b,c= largeAxis,smallAxis,border

for i = 1,360 do
vert(a,b,c,i-1)
vert(a,b,0,i-1)
vert(a,b,0,i)
vert(a,b,c,i-1)
vert(a,b,c,i)
vert(a,b,0,i)
end

ms.vertices = vertices
ms:setColors(77, 255, 0, 255)
return(ms)
end

function draw()
-- This sets a dark background color
background(40, 40, 50)
translate(WIDTH/2,HEIGHT/2)
rotate(30*ElapsedTime)
ms:draw()

end

``````

If you look inside the exported runtime under Project>Frameworks>RuntimeResources.bundle>shader.fsh, you’ll find all the shaders for all the rendering Codea does. For instance, the circle shader with stroke is:

``````
//
//  Codea
//
//  Created by Simeon Saint-Saëns on 17/05/11.
//

varying highp vec2 vTexCoord;

uniform lowp vec4 FillColor;
uniform lowp vec4 StrokeColor;

uniform highp float StrokeWidth;

void main()
{
//WORKING BETTER THAN OTHERS
/*
mediump vec2 aTexCoord = abs( vTexCoord );
mediump float angle = atan( aTexCoord.y, aTexCoord.x );

mediump float ryCosTheta = (Radius.y * cos(angle));
mediump float rxSinTheta = (Radius.x * sin(angle));
mediump float scaledRadius = (Radius.x * Radius.y) / sqrt( (ryCosTheta * ryCosTheta + rxSinTheta * rxSinTheta ) );

mediump float length = length( aTexCoord );
mediump float pixelLength = length * scaledRadius;

gl_FragColor = mix( mix( FillColor, StrokeColor, step(scaledRadius - StrokeWidth, pixelLength) ),
*/

//USING SUBTRACTION

highp vec2 innerRadiusAA = vec2( Radius.x - StrokeWidth * 2.0 - 4.0, Radius.y - StrokeWidth * 2.0 - 4.0 );

//Regular
//lowp vec4 fragCol = mix( FillColor, StrokeColor, smoothstep( cInner / cInnerAA, 1.0, cInner ) );

//Premult
lowp vec4 fragCol = mix( FillColor, StrokeColor, smoothstep( cInner / cInnerAA, 1.0, cInner ) );

//Regular alpha
//gl_FragColor = mix( fragCol, vec4(fragCol.rgb,0), smoothstep( c / cAA, 1.0, c ) );

//Premult
gl_FragColor = mix( fragCol, vec4(0,0,0,0), smoothstep( c / cAA, 1.0, c ) );
}
``````

Thank you