Toy Model - Building the Database

Our problem is to compute the area of a two-dimensional triangle defined within a square with 1 unit of edge length, based only in the triangle's points.

By this definition, we can ensure that the triangles' area vary from (0.01, 0.5] [1]


The database construction follows the steps:

  1. Set the random number generator: Mersenne Twister.
  2. Select the quadrant of the cartesian coordinate for creating the unit squares.
  3. Create random unit squares.
  4. Create random triangles for the train and test.

The unit square can be computed by multiplying a random number between 0 and 1 with the coordinate of the quadrant's limit, and using it as the minimum point for the random square.

  • Ex: In the first quadrant, from the coordinates (0,0) to (2,2), a unit square is selected at the minimum point (0.5, 0.5) and maximum at (1.5, 1.5).
  • Ex: In the third quadrant, from the coordinates (0,0) to (-3,-5), a unit square is selected at the minimum point (-1, -2.5) and maximum at (0, -1.5).

The triangle points can be computed by getting a random percentage of the size of the square edge and translating it to the minimum point of the square.


There are a few things to check so the triangles are valid:

  • The unit square respects the quadrant limits.
  • The squares are not bigger than the quadrant.
  • The triangle does not have coincident points, area below zero or above 0.5.
  • The triangles do not repeat through the database.


With that in mind, you can check the code. This implementation supports quadrants with minimum points different than (0,0), and non unit squares. Also, the execution time is counted so you can see the difference between the langueges. 

  • Python (slower)
  • C++ (very fast)



[1]: Note from the future: if the triangles are too small  (area below 0.01), they will be treated as outliers in the database. There are techniques to remove them, but since we build our database, we just limit it to be above 0.01


No comments:

Post a Comment

Global Game Jam 2024

Wow, what a weekend. The Global Game Jam is intense. But I and 3 other members manage to build a game for the theme "Make me laugh&quo...