3 ms·
There is another solution to the area/perimeter problem used as an example. Represent a shape as a series of edges and verticies. To compute the are of any sha
by phkahler 6y ago
There is another solution to the area/perimeter problem used as an example.
Represent a shape as a series of edges and verticies. To compute the are of any shape, sum the lengths of the edges. To find the area, sum the signed areas of the triangles formed by each edge and the origin. Works for all shapes, just define a new one from points and edges.
The point of TFA remains. What abstraction or data structure you select can determine what opertions on the data are easy or hard. This is why we need to study data structures and such - so you can choose the right one for the problem at hand, not so you can pick your favorite and shoehorn every problem into it.
- legerdemain 6y ago...But this isn't a matter of picking the right data structure, like swapping out a stack for a queue. The problem arises for elementary structs of finite size, simple data objects. If the list of operations a type needs to support never changes, of course it's more feasible to create a type that supports those operations. The actual area to learn about here is designing for extension, and software architecture in general, so you can get better at identifying what kinds of changes are going to be simple and what kind will be expensive, so you can tailor your solution to the range of situations you think you're going to face.
- phkahler 6y agoMy solution to represent shapes allows trivial extension in both way presented in TFA. Adding triangle simply requires some kind of definition in terms of what already exists. Adding a new measure (area/perimeter) means defining that once for what already exists. The fundamental mistake here is building code that reflects human concepts rather than more fundamental mathematical ones. Generalization is not the same as extensibility. Writing general purpose code is harder up front. Extending your code for every new shape leads to complexity problems.
- sp332 6y agoThat already doesn't work with circles though.
- phkahler 6y ago>> That already doesn't work with circles though. That depends on weather you want to use arc length and certain kinds of areas. Calculus is a thing too.
- gnulinux 6y agoThat doesn't make any sense. If you want to represent an ideal circle, calculus doesn't help, best you can do is pick an epsilon and add zillions of points to create an approximate circle. Even then most computational geometry algorithms won't work for that DS. If you're applying algebraic operations to that circle (let's say shear) you'll have to modify each and every single point (inefficient). What you want to do is to preserve a list of ideal (Platonic) shapes, apply transformations to them, and then convert this geometry to a list of vertices, edges and faces. Source: I've been a researcher on making parametric CAD tools that deal with problems like this for years.
- phkahler 6y agoArc length can be computed via integrals, but we dont really bother because breaking into segments is good enough (and elipses dont have a nice solution). For circular arcs, closed form solution exist even for the area. If you can accept the accuracy, the method of defining shapes from primitives is (to me) clearly simpler. It was of course just an example TFA used to highlight different approaches.
- phkahler 6y agoWe are on the same page. Dont use line segments, use NURBS curves. Length and area can be evaluated via piece wise approximation or numerical integration along the curve - which is a different flavor of the same thing.
- gnulinux 6y agoYeah I guess my point was at that point you'll have different APIs for square and circle.