You are here

function farm_area_generate_geometries in farmOS 7

Generate geometries within a polygon at a given orientation.

Parameters

$polygon: A Polygon geometry object that represents the parent area. Child area geometries will be generated within this polygon.

$count: The number of child geometries to generate.

$orientation: The orientation of child geometries, in degrees. This should be a positive integer between 0 and 359.

Return value

array Returns an array of child geometries.

2 calls to farm_area_generate_geometries()
farm_area_generate_form_preview in modules/farm/farm_area/farm_area_generate/farm_area_generate.module
Farm area generator form preview.
farm_area_generate_form_submit in modules/farm/farm_area/farm_area_generate/farm_area_generate.module
Farm area generate form submit.

File

modules/farm/farm_area/farm_area_generate/farm_area_generate.module, line 392
Farm area generate module.

Code

function farm_area_generate_geometries($polygon, $count, $orientation = 0) {

  // If the orientation isn't within an acceptable range, bail.
  if ($orientation < 0 || $orientation > 360) {
    return array();
  }

  // Set BCMath scale.
  farm_map_set_bcscale();

  // If the orientation is 360, reset to 0.
  if ($orientation == 360) {
    $orientation = 0;
  }

  // Rotate the polygon around its centroid so that the orientation is 0.
  $origin = $polygon
    ->centroid();
  $angle = 359 - $orientation;
  $rotated_polygon = farm_area_generate_rotate_polygon($polygon, $origin, $angle);

  // Calculate the bounding box of the rotated polygon.
  $bbox = $rotated_polygon
    ->getBBox();

  // Generate horizontal rectangles that fill the bounding box.
  $geometries = farm_area_generate_bbox_geometries($bbox, $count);

  // Rotate child geometries back to the original orientation and trim to fit
  // the original polygon.
  $final_geometries = array();
  foreach ($geometries as $geometry) {
    $rotated = farm_area_generate_rotate_polygon($geometry, $origin, $orientation);
    $trimmed = $rotated
      ->intersection($polygon);
    $final_geometries[] = $trimmed;
  }

  // Reset BCMath scale.
  farm_map_reset_bcscale();
  return $final_geometries;
}