/** * Starter code for the track ADT. * You're going to get crashes out of just about every test until you * fix track_add_point and track_get_point to at least respect the ownership * of the points passed back and forth, so fix that first! * * @author CPSC 223 Staff and you */ #include "track.h" #include #include typedef struct { size_t num_pts; trackpoint *pts[10]; // TO DO: make dynamically allocated to allow > 10 pts // TO DO: add whatever else you need here! } segment; // TO DO: implement some more segment functions; keep things modular! /** * Adds the given point to the given segment. * * @param seg a pointer to a segment, non-NULL * @param pt a pointer to a track point, non-NULL */ void segment_add_point(segment *seg, const trackpoint *pt); struct track { segment segments[1]; // TO DO: make dynamically allocated to allow > 1 seg // TO DO: add whatever other fields you need here! }; track *track_create() { // TO DO: much below will need to change as you add fields to the structs // and change from static arrays to dynamically allocated arrays track *trk = malloc(sizeof(*trk)); trk->segments[0].num_pts = 0; return trk; } void track_destroy(track *trk) { // TO DO: much will need to be added as you add fields to the structs // and change from static arrays to dynamically allocated arrays free(trk); } size_t track_count_segments(const track *trk) { // TO DO: change this after you implement track_start_segment return 1; } size_t track_count_points(const track *trk, size_t i) { // TO DO: change this after you update to allow more than one segment return trk->segments[0].num_pts; } trackpoint *track_get_point(const track *trk, size_t i, size_t j) { // TO DO: fix; this isn't even correct for one segment with one point // (read the documentation carefully) return trk->segments[i].pts[j]; } double *track_get_lengths(const track *trk) { // TO DO: implement this; this is just a "stub" to allow everything // to compile return NULL; } void track_add_point(track *trk, const trackpoint *pt) { // TO DO: update this to work as you change the structs to allow for // an unlimited number of points and segments segment_add_point(&trk->segments[0], pt); } void track_start_segment(track *trk) { // TO DO: replace this stub } void track_merge_segments(track *trk, size_t start, size_t end) { // TO DO: replace this stub too! } void track_heatmap(const track *trk, double cell_width, double cell_height, size_t ***map, size_t *rows, size_t *cols) { // TO DO: you got it -- this is yet another stub to replace } void segment_add_point(segment *seg, const trackpoint *pt) { // TO DO: allow for an arbitrary number of points per segment, and fix // that compiler warning about const (read the documentation of // track_add_point carefully) seg->pts[seg->num_pts++] = pt; }