I've been thinking about the design for a browser app on the HTML5 canvas that simulates a 2D robot zooming around, sensing the world around it. I decided to do this from scratch just for fun. I need shapes, like polygons, circles, and lines in order to model the robot and the world it lives in. These shapes need to be drawn with different appearance attributes, like border/fill style/width/color. I also need to have geometry functions to detect intersections and containment for the robot's sensors and so that the robot doesn't go inside stuff.
One idea for functions is to have two totally separate libraries, one to implement graphics (like drawShape(context, shape)
) and one for geometry operations (like shapeIntersectsShape(shape1, shape2)
). Or, in a more object-oriented approach, the shape objects themselves could implement methods to do their own graphics (shape.draw(context)
) and geometry operations (shape1.intersects(shape2)
).
Then there is the data itself: whether the data to draw a shape and the data to do geometric operations on that shape should be encapsulated within the same object, or be separate structures (where one would contain the other, or both be contained inside another structure).
How do existing applications that do graphics/geometry stuff deal with this? Is there one model that is best, or is each good for certain applications? Should the fact that I'm using Javascript instead of a more classical language change how I approach the design?