Different ecliptic values of from_altaz() and observe(sun).apparent().ecliptic_latlon() - python-3.x

At Sunrise sun is supposed to be at the horizon (ie 0 degree altitude at east).
but if I check ecliptic longitude of:
the sun at sunrise and
from_altaz() of zero degrees east
I get different values for both. But is it not supposed to be the same?
from skyfield import almanac, api
from pytz import timezone
from datetime import datetime, timedelta
ts = api.load.timescale(builtin=True)
eph = api.load('de421.bsp')
earth = eph['EARTH']
sun = eph['SUN']
tz = timezone('UTC')
# 2019-12-23 01:23:58.273000+00:00
testTime = datetime(year=2019, month=12, day=23, hour=1,
minute=23, second=58)
testTime = tz.localize(testTime)
lat = '33.775867N'
lon = '84.39733E'
observer = api.Topos(lat, lon)
t0 = ts.utc(testTime)
t1 = ts.utc(testTime + timedelta(days=1))
t, y = almanac.find_discrete(t0, t1, almanac.sunrise_sunset(eph, observer))
for i, j in zip(t, y):
if j:
print('Sunrise : ', i.utc_datetime().astimezone(tz), j)
observer = earth + api.Topos(lat, lon)
zeroDegreeEast = observer.at(t0).from_altaz(alt_degrees=0, az_degrees=90)
_, lonAtHorizon, _ = zeroDegreeEast.ecliptic_latlon(epoch=t0)
sunAtSunRise = observer.at(t0).observe(sun).apparent()
_, lonSun, _ = sunAtSunRise.ecliptic_latlon(epoch=t0)
print('TestDate: ', testTime.isoformat())
print(lonAtHorizon)
print(lonSun)
Sunrise : 2019-12-23 01:23:58.273000+00:00 True
TestDate: 2019-12-23T01:23:58+00:00
288deg 05' 48.3"
270deg 53' 48.0"
what am I missing?
Is there anything I could do to get the same values?

There are two additional effects that your script does not consider.
By definition, sunrise is when the center of the sun is 0.8333 degrees below the horizon, because it's half a degree wide and additionally its image is refracted upward and is visible ahead of schedule because of the atmosphere.
The sun never rises directly to the east, because of the seasons.
You can ask for the Sun's position on the horizon at the moment of sunrise that you've computed to account for the second effect:
alt, az, distance = ((observer + eph['earth']).at(t[0])
.observe(eph['sun']).apparent().altaz())
print(alt, az)
print(az.degrees)
Which prints:
-00deg 49' 59.9" 117deg 57' 17.6"
117.95487601490409
or about 27° south of directly east. You can then account for the first effect by adjusting the altaz you are computing:
zeroDegreeEast = observer.at(t0).from_altaz(alt_degrees=-0.8333, az_degrees=117.954)
The result should very closely match what you expect.

Related

Lack of perspective/distance in 3d projection. What am I doing wrong?

transform3Dpoint2D(var/px, var/py, var/pz)
//perform the rotations around each axis
//rotation around x
var/xy = cx*py - sx*pz
var/xz = sx*py + cx*pz
//rotation around y
var/yz = cy*xz - sy*px
var/yx = sy*xz + cy*px
//rotation around z
var/zx = cz*yx - sz*xy
var/zy = sz*yx + cz*xy
//return variables: x, y, how close point is (for sorting)
var/scaleRatio = 300/(300 + yz)
return list(zx*scaleRatio, zy*scaleRatio, yz)
The variables (cameraxr, camerayr, camerazr are pitch, yaw, roll respectively):
//Setup projection variables
sx = sin(cameraxr)
cx = cos(cameraxr)
sy = sin(camerayr)
cy = cos(camerayr)
sz = sin(camerazr)
cz = cos(camerazr)
When I use this code to project 3d coordinates into 2d, it works fine, but there seems to be a lack of perspective entirely. The objects behind should be smaller, and the objects on the front should be more bigger. What am I missing here? The top should also be seeen when it's moved down. It looks like this when used:

FiPy Setting outflow condition the correct way

I need some help with a quiete simple problem in FiPy. My goal is to simulate a fluid flowing through a concrete block while phase change.
But first of all I tried to do a simple 1D simulation assumed a fluid massflow and a constant wall temperature without any phase change.
from fipy import *
from fipy.meshes import CylindricalGrid2D, Grid1D
import matplotlib.pyplot as plt
import numpy as np
#%%
L = 1.5 #length transfer surface
bS = 0.75 #wide
AV = L * bS #transfer surface
tS0 = 350. #tWall
rhoWF = 880. #density fluid
mWF = 0.036 #mass flow
u = 5e-4 #Fluid speed
hWF = mWF / AV / rhoWF / u #height "fluid block"
nx = 50
VWF = hWF * L * bS/nx #fluid volumen
lambdaWF = 0.6 # thermal conductivity
alpha = 500. #heat transfer coefficient
tWF0 = 371.
mesh = Grid1D(dx=L/nx, nx=nx)
tWF = CellVariable(name="Fluid",
mesh=mesh,
value= tWF0,
hasOld=True)
tS = CellVariable(name="storage",
mesh=mesh,
value=tS0,
hasOld=True)
sourceWF=CellVariable(name="source Fluid", #Variable der Konvektion
mesh=mesh,
value=0.)
cvrho = CellVariable(name = 'cprho',#Fluid
mesh = mesh,
value = rhoWF * 4215.2,
hasOld = True)
tWF.constrain(tWF0, mesh.facesLeft()) #constant inlet temperature
t = 6*3600. #time
timeStepDuration = 1e2
#outflow boundary condition
outlet = mesh.facesRight
ConvCoeff = FaceVariable(mesh,value=u,rank=1)
exteriorCoeff = FaceVariable(mesh,value=0.,rank=1)
exteriorCoeff.setValue(value=ConvCoeff, where=outlet)
ConvCoeff.setValue(0., where=outlet)
residual1 = 1.
elapsedTime = 0.
tWFall = np.zeros(nx)[None,:]
while elapsedTime < t:
tWF.updateOld()
it = 0 #iterations
while residual1> 1e-2:
sourceWF.value = - AV / nx * alpha*(tWF - tS)/ cvrho / VWF #this will be a variable convection source
eq1 = HybridConvectionTerm(coeff=ConvCoeff) + TransientTerm(coeff=1.) == \
+ sourceWF\
- ImplicitSourceTerm(exteriorCoeff.divergence) \
#+ DiffusionTerm(coeff= lambdaWF / cvrho) #not necessary(?)
residual1 = eq1.sweep(dt = timeStepDuration, var = tWF)
print('res1: ' + str(residual1) )
it += 1
if it > 10:
raise ValueError (r'MaxIter reached')
elapsedTime += timeStepDuration ; print('t= ' + str(round(elapsedTime,2)))
residual1 = 1.
tWFall = np.r_[tWFall, tWF.value[None,:]] #value collection
#%% outlet fluid temperature and storage temperature
plt.plot(np.linspace(0,t/3600.,int(t/timeStepDuration)), tWFall[1:,-1], label=r'$\vartheta_{WF}$')
plt.legend()
I would expect a constant fluid outlet temperature because of the constant wall temperature and constant fluid inlet temperature. I have not defined the wall temperature as a boundary condition because some day I would like to analyse heat conduction and variable temperature gradients too. Running my mwe you can see that the fluid temperature at the outlet declines.
Could someone please help at this case?
Thanks in advance!
I changed the script around and it seem to give a constant temperature of 371.0. See this link.
The sourceWF term has been removed. I was unsure what this was for, but I think it would take time for the wall temperature to adjust to this.
The equation declaration has been moved outside the loop. This is the correct way to use FiPy, but shouldn't impact the results in this case.

Corona Lua - how to scale and rotate a large image with multitouch

I'm working with Corona and Lua script.
basically I want to scale and rotate a 'larger than screen' image with dual-touch
Please help :)
Try this
-- one more thing
-- turn on multitouch
system.activate("multitouch")
-- which environment are we running on?
local isDevice = (system.getInfo("environment") == "device")
-- returns the distance between points a and b
function lengthOf( a, b )
local width, height = b.x-a.x, b.y-a.y
return (width*width + height*height)^0.5
end
-- returns the degrees between (0,0) and pt
-- note: 0 degrees is 'east'
function angleOfPoint( pt )
local x, y = pt.x, pt.y
local radian = math.atan2(y,x)
local angle = radian*180/math.pi
if angle < 0 then angle = 360 + angle end
return angle
end
-- returns the degrees between two points
-- note: 0 degrees is 'east'
function angleBetweenPoints( a, b )
local x, y = b.x - a.x, b.y - a.y
return angleOfPoint( { x=x, y=y } )
end
-- returns the smallest angle between the two angles
-- ie: the difference between the two angles via the shortest distance
function smallestAngleDiff( target, source )
local a = target - source
if (a > 180) then
a = a - 360
elseif (a < -180) then
a = a + 360
end
return a
end
-- rotates a point around the (0,0) point by degrees
-- returns new point object
function rotatePoint( point, degrees )
local x, y = point.x, point.y
local theta = math.rad( degrees )
local pt = {
x = x * math.cos(theta) - y * math.sin(theta),
y = x * math.sin(theta) + y * math.cos(theta)
}
return pt
end
-- rotates point around the centre by degrees
-- rounds the returned coordinates using math.round() if round == true
-- returns new coordinates object
function rotateAboutPoint( point, centre, degrees, round )
local pt = { x=point.x - centre.x, y=point.y - centre.y }
pt = rotatePoint( pt, degrees )
pt.x, pt.y = pt.x + centre.x, pt.y + centre.y
if (round) then
pt.x = math.round(pt.x)
pt.y = math.round(pt.y)
end
return pt
end
-- calculates the average centre of a list of points
local function calcAvgCentre( points )
local x, y = 0, 0
for i=1, #points do
local pt = points[i]
x = x + pt.x
y = y + pt.y
end
return { x = x / #points, y = y / #points }
end
-- calculate each tracking dot's distance and angle from the midpoint
local function updateTracking( centre, points )
for i=1, #points do
local point = points[i]
point.prevAngle = point.angle
point.prevDistance = point.distance
point.angle = angleBetweenPoints( centre, point )
point.distance = lengthOf( centre, point )
end
end
-- calculates rotation amount based on the average change in tracking point rotation
local function calcAverageRotation( points )
local total = 0
for i=1, #points do
local point = points[i]
total = total + smallestAngleDiff( point.angle, point.prevAngle )
end
return total / #points
end
-- calculates scaling amount based on the average change in tracking point distances
local function calcAverageScaling( points )
local total = 0
for i=1, #points do
local point = points[i]
total = total + point.distance / point.prevDistance
end
return total / #points
end
-- creates an object to be moved
function newTrackDot(e)
-- create a user interface object
local circle = display.newCircle( e.x, e.y, 50 )
-- make it less imposing
circle.alpha = .5
-- keep reference to the rectangle
local rect = e.target
-- standard multi-touch event listener
function circle:touch(e)
-- get the object which received the touch event
local target = circle
-- store the parent object in the event
e.parent = rect
-- handle each phase of the touch event life cycle...
if (e.phase == "began") then
-- tell corona that following touches come to this display object
display.getCurrentStage():setFocus(target, e.id)
-- remember that this object has the focus
target.hasFocus = true
-- indicate the event was handled
return true
elseif (target.hasFocus) then
-- this object is handling touches
if (e.phase == "moved") then
-- move the display object with the touch (or whatever)
target.x, target.y = e.x, e.y
else -- "ended" and "cancelled" phases
-- stop being responsible for touches
display.getCurrentStage():setFocus(target, nil)
-- remember this object no longer has the focus
target.hasFocus = false
end
-- send the event parameter to the rect object
rect:touch(e)
-- indicate that we handled the touch and not to propagate it
return true
end
-- if the target is not responsible for this touch event return false
return false
end
-- listen for touches starting on the touch layer
circle:addEventListener("touch")
-- listen for a tap when running in the simulator
function circle:tap(e)
if (e.numTaps == 2) then
-- set the parent
e.parent = rect
-- call touch to remove the tracking dot
rect:touch(e)
end
return true
end
-- only attach tap listener in the simulator
if (not isDevice) then
circle:addEventListener("tap")
end
-- pass the began phase to the tracking dot
circle:touch(e)
-- return the object for use
return circle
end
-- spawning tracking dots
-- create display group to listen for new touches
local group = display.newGroup()
-- populate display group with objects
local rect = display.newRect( group, 200, 200, 200, 100 )
rect:setFillColor(0,0,255)
rect = display.newRect( group, 300, 300, 200, 100 )
rect:setFillColor(0,255,0)
rect = display.newRect( group, 100, 400, 200, 100 )
rect:setFillColor(255,0,0)
-- keep a list of the tracking dots
group.dots = {}
-- advanced multi-touch event listener
function touch(self, e)
-- get the object which received the touch event
local target = e.target
-- get reference to self object
local rect = self
-- handle began phase of the touch event life cycle...
if (e.phase == "began") then
print( e.phase, e.x, e.y )
-- create a tracking dot
local dot = newTrackDot(e)
-- add the new dot to the list
rect.dots[ #rect.dots+1 ] = dot
-- pre-store the average centre position of all touch points
rect.prevCentre = calcAvgCentre( rect.dots )
-- pre-store the tracking dot scale and rotation values
updateTracking( rect.prevCentre, rect.dots )
-- we handled the began phase
return true
elseif (e.parent == rect) then
if (e.phase == "moved") then
print( e.phase, e.x, e.y )
-- declare working variables
local centre, scale, rotate = {}, 1, 0
-- calculate the average centre position of all touch points
centre = calcAvgCentre( rect.dots )
-- refresh tracking dot scale and rotation values
updateTracking( rect.prevCentre, rect.dots )
-- if there is more than one tracking dot, calculate the rotation and scaling
if (#rect.dots > 1) then
-- calculate the average rotation of the tracking dots
rotate = calcAverageRotation( rect.dots )
-- calculate the average scaling of the tracking dots
scale = calcAverageScaling( rect.dots )
-- apply rotation to rect
rect.rotation = rect.rotation + rotate
-- apply scaling to rect
rect.xScale, rect.yScale = rect.xScale * scale, rect.yScale * scale
end
-- declare working point for the rect location
local pt = {}
-- translation relative to centre point move
pt.x = rect.x + (centre.x - rect.prevCentre.x)
pt.y = rect.y + (centre.y - rect.prevCentre.y)
-- scale around the average centre of the pinch
-- (centre of the tracking dots, not the rect centre)
pt.x = centre.x + ((pt.x - centre.x) * scale)
pt.y = centre.y + ((pt.y - centre.y) * scale)
-- rotate the rect centre around the pinch centre
-- (same rotation as the rect is rotated!)
pt = rotateAboutPoint( pt, centre, rotate, false )
-- apply pinch translation, scaling and rotation to the rect centre
rect.x, rect.y = pt.x, pt.y
-- store the centre of all touch points
rect.prevCentre = centre
else -- "ended" and "cancelled" phases
print( e.phase, e.x, e.y )
-- remove the tracking dot from the list
if (isDevice or e.numTaps == 2) then
-- get index of dot to be removed
local index = table.indexOf( rect.dots, e.target )
-- remove dot from list
table.remove( rect.dots, index )
-- remove tracking dot from the screen
e.target:removeSelf()
-- store the new centre of all touch points
rect.prevCentre = calcAvgCentre( rect.dots )
-- refresh tracking dot scale and rotation values
updateTracking( rect.prevCentre, rect.dots )
end
end
return true
end
-- if the target is not responsible for this touch event return false
return false
end
-- attach pinch zoom touch listener
group.touch = touch
-- listen for touches starting on the touch object
group:addEventListener("touch")
Google search for "corona sdk zoom": implementing pinch zoom rotate

python3 reportlab code 39 adding extra character

Using python3 and reportlab I'm trying to generate two columns of barcodes:
from reportlab.graphics.barcode import code39
from reportlab.lib.pagesizes import A4
from reportlab.lib.units import mm
from reportlab.pdfgen import canvas
c = canvas.Canvas("barcode_example.pdf", pagesize=A4)
code_list = [
'E100', 'RA100',
'E101', 'RA101',
'E102', 'RA102',
'E103', 'RA103',
'E104', 'RA104',
'E105', 'RA105',
'E106', 'RA106',
'E107', 'RA107',
'E108', 'RA108',
'E109', 'RA109']
x = 1 * mm
y = 278 * mm
x1 = 6.4 * mm
for i, code in enumerate(code_list):
barcode = code39.Extended39(code, barWidth=0.6*mm, barHeight=15*mm)
if i % 2 == 0:
barcode.drawOn(c, x, y)
x1 = x + 6.4 * mm
c.drawString(x1, y- 5 * mm, code)
else:
x1 = x + 100 * mm
barcode.drawOn(c, x1, y)
y = y - 5 * mm
c.drawString(x1 + 6.4 * mm, y, code)
y = y - 25 * mm
# if int(y) == 0:
# x = x + 50 * mm
# y = 285 * mm
c.showPage()
c.save()
It generates a file and seems to be just fine but after scanning them in with a barcode scanner they read as follows:
E100F RA100
E101G RA101$
E102H RA102/
E103I RA103+
E104J RA104%
E105K RA1050
E106L RA1061
E107M RA1072
E108N RA1083
E109O RA1094
RA100 has a space after it.
It is clearly going in order of ASCII precedence I just don't know why. Is there a setting I'm suppose to enable/disable?
Also, it's definitely not the barcode scanner, I reset it to read code39 and tested it on other non-reportlab generated barcodes and they worked just fine.
Thank you for your help!
Also, I modified the code from here: https://stackoverflow.com/questions/2179269/python-barcode-generation-library
Edit
When I tested with code128, it worked perfectly. So it appears to be specific to code39
EDIT 2: And I'm an idiot. My barcode scanner doesn't support the checksum option. Disabling the checksum in reportlab fixed my issue. I'm going to leave this just in case someone else comes across this problem.
Disable checksum via:
barcode = code39.Extended39(code, barWidth=0.6*mm, barHeight=15*mm, checksum=0)
checksum=False would probably work just fine, but after looking at their source they use 1 and 0 for true/false and figured it would be safer to adhere to their practices.

How do I convert the 2 control points of a cubic curve to the single control point of a quadratic curve?

Having searched the web, I see various people in various forums alluding to approximating a cubic curve with a quadratic one. But I can't find the formula.
What I want is this:
input: startX, startY, control1X, control1Y, control2X, control2Y, endX, endY
output: startX, startY, controlX, controlY, endX, endY
Actually, since the starting and ending points will be the same, all I really need is...
input: startX, startY, control1X, control1Y, control2X, control2Y, endX, endY
output: controlX, controlY
As mentioned, going from 4 control points to 3 is normally going to be an approximation. There's only one case where it will be exact - when the cubic bezier curve is actually a degree-elevated quadratic bezier curve.
You can use the degree elevation equations to come up with an approximation. It's simple, and the results are usually pretty good.
Let's call the control points of the cubic Q0..Q3 and the control points of the quadratic P0..P2. Then for degree elevation, the equations are:
Q0 = P0
Q1 = 1/3 P0 + 2/3 P1
Q2 = 2/3 P1 + 1/3 P2
Q3 = P2
In your case you have Q0..Q3 and you're solving for P0..P2. There are two ways to compute P1 from the equations above:
P1 = 3/2 Q1 - 1/2 Q0
P1 = 3/2 Q2 - 1/2 Q3
If this is a degree-elevated cubic, then both equations will give the same answer for P1. Since it's likely not, your best bet is to average them. So,
P1 = -1/4 Q0 + 3/4 Q1 + 3/4 Q2 - 1/4 Q3
To translate to your terms:
controlX = -0.25*startX + .75*control1X + .75*control2X -0.25*endX
Y is computed similarly - the dimensions are independent, so this works for 3d (or n-d).
This will be an approximation. If you need a better approximation, one way to get it is by subdividing the initial cubic using the deCastlejau algorithm, and then degree-reduce each segment. If you need better continuity, there are other approximation methods that are less quick and dirty.
The cubic can have loops and cusps, which quadratic cannot have. This means that there are not simple solutions nearly never. If cubic is already a quadratic, then the simple solution exists. Normally you have to divide cubic to parts that are quadratics. And you have to decide what are the critical points for subdividing.
http://fontforge.org/bezier.html#ps2ttf says:
"Other sources I have read on the net suggest checking the cubic spline for points of inflection (which quadratic splines cannot have) and forcing breaks there. To my eye this actually makes the result worse, it uses more points and the approximation does not look as close as it does when ignoring the points of inflection. So I ignore them."
This is true, the inflection points (second derivatives of cubic) are not enough. But if you take into account also local extremes (min, max) which are the first derivatives of cubic function, and force breaks on those all, then the sub curves are all quadratic and can be presented by quadratics.
I tested the below functions, they work as expected (find all critical points of cubic and divides the cubic to down-elevated cubics). When those sub curves are drawn, the curve is exactly the same as original cubic, but for some reason, when sub curves are drawn as quadratics, the result is nearly right, but not exactly.
So this answer is not for strict help for the problem, but those functions provide a starting point for cubic to quadratic conversion.
To find both local extremes and inflection points, the following get_t_values_of_critical_points() should provide them. The
function compare_num(a,b) {
if (a < b) return -1;
if (a > b) return 1;
return 0;
}
function find_inflection_points(p1x,p1y,p2x,p2y,p3x,p3y,p4x,p4y)
{
var ax = -p1x + 3*p2x - 3*p3x + p4x;
var bx = 3*p1x - 6*p2x + 3*p3x;
var cx = -3*p1x + 3*p2x;
var ay = -p1y + 3*p2y - 3*p3y + p4y;
var by = 3*p1y - 6*p2y + 3*p3y;
var cy = -3*p1y + 3*p2y;
var a = 3*(ay*bx-ax*by);
var b = 3*(ay*cx-ax*cy);
var c = by*cx-bx*cy;
var r2 = b*b - 4*a*c;
var firstIfp = 0;
var secondIfp = 0;
if (r2>=0 && a!==0)
{
var r = Math.sqrt(r2);
firstIfp = (-b + r) / (2*a);
secondIfp = (-b - r) / (2*a);
if ((firstIfp>0 && firstIfp<1) && (secondIfp>0 && secondIfp<1))
{
if (firstIfp>secondIfp)
{
var tmp = firstIfp;
firstIfp = secondIfp;
secondIfp = tmp;
}
if (secondIfp-firstIfp >0.00001)
return [firstIfp, secondIfp];
else return [firstIfp];
}
else if (firstIfp>0 && firstIfp<1)
return [firstIfp];
else if (secondIfp>0 && secondIfp<1)
{
firstIfp = secondIfp;
return [firstIfp];
}
return [];
}
else return [];
}
function get_t_values_of_critical_points(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y) {
var a = (c2x - 2 * c1x + p1x) - (p2x - 2 * c2x + c1x),
b = 2 * (c1x - p1x) - 2 * (c2x - c1x),
c = p1x - c1x,
t1 = (-b + Math.sqrt(b * b - 4 * a * c)) / 2 / a,
t2 = (-b - Math.sqrt(b * b - 4 * a * c)) / 2 / a,
tvalues=[];
Math.abs(t1) > "1e12" && (t1 = 0.5);
Math.abs(t2) > "1e12" && (t2 = 0.5);
if (t1 >= 0 && t1 <= 1 && tvalues.indexOf(t1)==-1) tvalues.push(t1)
if (t2 >= 0 && t2 <= 1 && tvalues.indexOf(t2)==-1) tvalues.push(t2);
a = (c2y - 2 * c1y + p1y) - (p2y - 2 * c2y + c1y);
b = 2 * (c1y - p1y) - 2 * (c2y - c1y);
c = p1y - c1y;
t1 = (-b + Math.sqrt(b * b - 4 * a * c)) / 2 / a;
t2 = (-b - Math.sqrt(b * b - 4 * a * c)) / 2 / a;
Math.abs(t1) > "1e12" && (t1 = 0.5);
Math.abs(t2) > "1e12" && (t2 = 0.5);
if (t1 >= 0 && t1 <= 1 && tvalues.indexOf(t1)==-1) tvalues.push(t1);
if (t2 >= 0 && t2 <= 1 && tvalues.indexOf(t2)==-1) tvalues.push(t2);
var inflectionpoints = find_inflection_points(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y);
if (inflectionpoints[0]) tvalues.push(inflectionpoints[0]);
if (inflectionpoints[1]) tvalues.push(inflectionpoints[1]);
tvalues.sort(compare_num);
return tvalues;
};
And when you have those critical t values (which are from range 0-1), you can divide the cubic to parts:
function CPoint()
{
var arg = arguments;
if (arg.length==1)
{
this.X = arg[0].X;
this.Y = arg[0].Y;
}
else if (arg.length==2)
{
this.X = arg[0];
this.Y = arg[1];
}
}
function subdivide_cubic_to_cubics()
{
var arg = arguments;
if (arg.length!=9) return [];
var m_p1 = {X:arg[0], Y:arg[1]};
var m_p2 = {X:arg[2], Y:arg[3]};
var m_p3 = {X:arg[4], Y:arg[5]};
var m_p4 = {X:arg[6], Y:arg[7]};
var t = arg[8];
var p1p = new CPoint(m_p1.X + (m_p2.X - m_p1.X) * t,
m_p1.Y + (m_p2.Y - m_p1.Y) * t);
var p2p = new CPoint(m_p2.X + (m_p3.X - m_p2.X) * t,
m_p2.Y + (m_p3.Y - m_p2.Y) * t);
var p3p = new CPoint(m_p3.X + (m_p4.X - m_p3.X) * t,
m_p3.Y + (m_p4.Y - m_p3.Y) * t);
var p1d = new CPoint(p1p.X + (p2p.X - p1p.X) * t,
p1p.Y + (p2p.Y - p1p.Y) * t);
var p2d = new CPoint(p2p.X + (p3p.X - p2p.X) * t,
p2p.Y + (p3p.Y - p2p.Y) * t);
var p1t = new CPoint(p1d.X + (p2d.X - p1d.X) * t,
p1d.Y + (p2d.Y - p1d.Y) * t);
return [[m_p1.X, m_p1.Y, p1p.X, p1p.Y, p1d.X, p1d.Y, p1t.X, p1t.Y],
[p1t.X, p1t.Y, p2d.X, p2d.Y, p3p.X, p3p.Y, m_p4.X, m_p4.Y]];
}
subdivide_cubic_to_cubics() in above code divides an original cubic curve to two parts by the value t. Because get_t_values_of_critical_points() returns t values as an array sorted by t value, you can easily traverse all t values and get the corresponding sub curve. When you have those divided curves, you have to divide the 2nd sub curve by the next t value.
When all splitting is proceeded, you have the control points of all sub curves. Now there are left only the cubic control point conversion to quadratic. Because all sub curves are now down-elevated cubics, the corresponding quadratic control points are easy to calculate. The first and last of quadratic control points are the same as cubic's (sub curve) first and last control point and the middle one is found in the point, where lines P1-P2 and P4-P3 crosses.
Conventions/terminology
Cubic defined by: P1/2 - anchor points, C1/C2 control points
|x| is the euclidean norm of x
mid-point approx of cubic: a quad that shares the same anchors with the cubic and has the control point at C = (3·C2 - P2 + 3·C1 - P1)/4
Algorithm
pick an absolute precision (prec)
Compute the Tdiv as the root of (cubic) equation sqrt(3)/18 · |P2 - 3·C2 + 3·C1 - P1|/2 · Tdiv ^ 3 = prec
if Tdiv < 0.5 divide the cubic at Tdiv. First segment [0..Tdiv] can be approximated with by a quadratic, with a defect less than prec, by the mid-point approximation. Repeat from step 2 with the second resulted segment (corresponding to 1-Tdiv)
0.5<=Tdiv<1 - simply divide the cubic in two. The two halves can be approximated by the mid-point approximation
Tdiv>=1 - the entire cubic can be approximated by the mid-point approximation
The "magic formula" at step 2 is demonstrated (with interactive examples) on this page.
Another derivation of tfinniga's answer:
First see Wikipedia Bezier curve
for the formulas for quadratic and cubic Bezier curves (also nice animations):
Q(t) = (1-t)^2 P0 + 2 (1-t) t Q + t^2 P3
P(t) + (1-t)^3 P0 + 3 (1-t)^2 t P1 + 3 (1-t) t^2 P2 + t^3 P3
Require these to match at the middle, t = 1/2:
(P0 + 2 Q + P3) / 4 = (P0 + 3 P1 + 3 P2 + P3) / 8
=> Q = P1 + P2 - (P0 + P1 + P2 + P3) / 4
(Q written like this has a geometric interpretation:
Pmid = middle of P0 P1 P2 P3
P12mid = midway between P1 and P2
draw a line from Pmid to P12mid, and that far again: you're at Q.
Hope this makes sense -- draw a couple of examples.)
In general, you'll have to use multiple quadratic curves - many cases of cubic curves can't be even vaguely approximated with a single quadratic curve.
There is a good article discussing the problem, and a number of ways to solve it, at http://www.timotheegroleau.com/Flash/articles/cubic_bezier_in_flash.htm (including interactive demonstrations).
I should note that Adrian's solution is great for single cubics, but when the cubics are segments of a smooth cubic spline, then using his midpoint approximation method causes slope continuity at the nodes of the segments to be lost. So the method described at http://fontforge.org/bezier.html#ps2ttf is much better if you are working with font glyphs or for any other reason you want to retain the smoothness of the curve (which is most probably the case).
Even though this is an old question, many people like me will see it in search results, so I'm posting this here.
I would probably draw a series of curves instead of trying to draw one curve using a different alg. Sort of like drawing two half circles to make up a whole circle.
Try looking for opensource Postcript font to Truetype font converters. I'm sure they have it. Postscript uses cubic bezier curves, whereas Truetype uses quadratic bezier curves. Good luck.

Resources