Skip to content

Instantly share code, notes, and snippets.

class f(object):
def __init__(self, p1, p2, p3):
self.p1 = p1
self.p2 = p2
self.p3 = p3
def __call__(self, t):
ret = list(self.p1)
for i in range(len(ret)):
ret[i] = (1 - t ** 2) * self.p1[i] + 2 * t * (1 - t) * self.p2[i] + t ** 2 * self.p3[i]
return tuple(ret)
def draw_curve(curve):
if is_flat(curve):
draw_segments(curve)
else:
pieces = subdivide(curve)
draw_curve(pieces[0])
draw_curve(pieces[1])
def midpoint(p1, p2):
ret = list(p1)
for i in range(len(ret)):
ret[i] = (p1[i] + p2[i]) / 2.
return tuple(ret)
def midpoints(points):
ret = points[:-1]
for i in range(len(ret)):
ret[i] = midpoint(points[i], points[i+1])
def is_flat(curve):
tol = 25
ax = 3. * curve[1][0] - 2. * curve[0][0] - curve[3][0]
ay = 3. * curve[1][1] - 2. * curve[0][1] - curve[3][1]
bx = 3. * curve[2][0] - curve[0][0] - 2. * curve[3][0]
by = 3. * curve[2][1] - curve[0][1] - 2. * curve[3][1]
return max(ax*ax, bx*bx) + max(ay*ay, by*by) <= tol
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
void f(const char *filename)
{
// check some conditions
if (!filename)
return;
// ... acquire some resources
int *array = malloc(sizeof(int) * /* ... */);
if (!array)
return;
Buffer buffer = new Buffer();
while (true) {
String in = System.console.readLine("...");
if (in.equals("*"))
break;
byte buf[] = new byte[1000000];
buffer.add(buf);
// do something with buf
}
def mark(objects):
for obj in objects:
if !obj.isMarked():
obj.setMark(True)
mark(obj.children)
mark(root)