#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

# Has somebody an ellipse mesh code ?

edited August 2013 Posts: 30

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

Tagged:

## Comments

• edited August 2013 Posts: 3,295

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

``````-- ellipse mesh

function setup()
ms = mesh()
local x,y,i,angle
local cos,sin,rad = math.cos,math.sin,math.rad
local a,b = 100,200
local vertices = {}
for i = 1,360 do
table.insert(vertices,vec3(0,0,0))
angle = rad(i-1)
x = a*cos(angle)
y = b*sin(angle)
table.insert(vertices,vec3(x,y,0))
angle = rad(i)
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
``````
• edited August 2013 Posts: 1,976

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
img = readImage("Cargo Bot:Codea Icon")
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
local nrad = rad + (math.pi / precision)
table.insert(v, vec2(w * fs(rad), h * fc(rad)))
table.insert(v, vec2(w / 2, h / 2))
table.insert(v, vec2(w * fs(nrad), h * fc(nrad)))
table.insert(tc, vec2(fs(rad), fc(rad)))
table.insert(tc, vec2(0.5, 0.5))
table.insert(tc, vec2(fs(nrad), fc(nrad)))
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.

• Posts: 30

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 ?

• Posts: 30

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

• edited August 2013 Posts: 3,295

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 cos,sin,rad = math.cos,math.sin,math.rad
local vertices = {}

local function vert(a,b,c,i)
local angle = rad(i-1)
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

``````
• Posts: 2,820

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:

``````//
//  Shader.fsh
//  Codea
//
//  Created by Simeon Saint-Saëns on 17/05/11.
//  Copyright 2011 Two Lives Left. All rights reserved.
//

varying highp vec2 vTexCoord;

uniform lowp vec4 FillColor;
uniform lowp vec4 StrokeColor;

uniform highp vec2 Radius;
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 scaledRadius = mix( Radius.x, Radius.y, angle / (3.14159265 * 0.5) );
mediump float pixelLength = length * scaledRadius;

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

//USING SUBTRACTION
highp vec2 RadiusAA = vec2(Radius.x - 4.0, Radius.y - 4.0);

highp vec2 scaledPointSq = vec2( (vTexCoord.x * Radius.x) * (vTexCoord.x * Radius.x), (vTexCoord.y * Radius.y) * (vTexCoord.y * Radius.y) );

highp float c = (scaledPointSq.x / (Radius.x*Radius.x)) + (scaledPointSq.y / (Radius.y*Radius.y));
highp float cAA = (scaledPointSq.x / (RadiusAA.x*RadiusAA.x)) + (scaledPointSq.y / (RadiusAA.y*RadiusAA.y));

highp vec2 innerRadius = vec2( Radius.x - StrokeWidth * 2.0, Radius.y - StrokeWidth * 2.0 );
highp vec2 innerRadiusAA = vec2( Radius.x - StrokeWidth * 2.0 - 4.0, Radius.y - StrokeWidth * 2.0 - 4.0 );
highp float cInner = (scaledPointSq.x / (innerRadius.x*innerRadius.x)) + (scaledPointSq.y / (innerRadius.y*innerRadius.y));
highp float cInnerAA = (scaledPointSq.x / (innerRadiusAA.x*innerRadiusAA.x)) + (scaledPointSq.y / (innerRadiusAA.y*innerRadiusAA.y));

//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 ) );
}
``````
• Posts: 30

Thank you Sign In or Register to comment.