System

For this week’s homework. I refactored my week 2 assignment attractor using inheritance. During the process, I found myself unsure about how to structure the relationship between classes through inheritance.

For example, it makes sense to extend Attractor class from Particle class, as attractors are particles that can exert force. However, When I was adding Repeller class, I was confused if I should extend from Particle class or Attractor class. Because repellers are very similar to attractors in behavior, except the force exerted is in the opposite direction. So if I extend from Attractor class, I could just call super class’ force function and change direction. Conceptually, it makes more sense to categorize it as a child of Particle rather than Attractor. I think I am confused inheritance with interface. What should I consider then I am making a subclass? The overall conceptual connection of the inheritance or reusable functionalities? What are the issues when using inheritance carelessly?

Once I rewrote the attractor system, I wanted the system to manage adding attractors to the field rather than a person manually doing that. So I used setInterval to periodically add an attractor to the canvas. The placement is calculated based the (width/2+offset, random(0, height)). The system still needs better rule for placing the attractors. Maybe checking the existing attractors’ position, calculate the new attractor’s position relative to previous attractor’s position. Also, I could experiment with removing the attractors. Here are some of the images produced by attractor system:

attractor

attractor

attractor

attractor

You can see the project in action here.

You might also enjoy