% Part B - Some functions from part A have changed, and the description % for them is updated here. % % New functions have been added to support the 5 extensions. You should % create the functions as listed here, but your team may choose not % to implement all of the extensions. For instance, if you choose not % to implement collisions, you do not have to call processCollisions % from the drawAndUpdateEveryCircle function. % % Additionally each new function is marked as USED BY: EXTENSION % to make it clear which functions need to be implemented for each % project extension. Note that the trails extension does not require % a new function, but it does require changing the handles vector % to a two dimensional matrix -- which means changes to many % different functions. %For each of the following functions, your TEAM will write the contract, a complete %description, and examples that work for testing. You will also write %test functions for every function here. Tests for functions that draw %are difficult to test directly, so you may carefully describe figure test %results so that any observer can see if the tests pass. %Draws each %circle from parameter vector once to get handles. For a given %number of cycles, draws and updates every circle structure in the parameter %vector. Pauses for a given pauseTime each iteration to simulate animation. % % Trails - creates a two dimensional matrix of handles % Splitting - checks if circles is empty (might end % simulation before cycles are completed) % %calls: makeBoard, drawCircles, drawAndUpdateEveryCircle %USED BY: ALL function [] = project1b(circles,board, cycles, pauseTime) %Creates and returns a board structure % USED BY: ALL function board = makeBoard(xmin, xmax, ymin, ymax, gravity, viscosity, trails, splitting) %Begins by processing circle splits, collisions, and removes small circles. %Then for each circle, checks to see if circle is touching edge of board. If %it is, alter the x and/or y velocity accordingly. Then draw the circle, %keeping the new handle. Be sure to delete the old handle to the previous %version of this circle. Update the position of the circle according to %its velocity data. Update the velocity of the circle for both gravity and %friction. % %calls: processCircleSplits, processCollisions, removeSmallCircles, % atEdge, reflectCircle, drawCircle, updateCirclePosition, % updateCircleGravity, updateCircleFriction % delete (built-in) % USED BY: ALL function [circleVector handleVector] = drawAndUpdateEveryCircle(circles, handles, board) % Updates y velocity of the circle using gravity. % USED BY: GRAVITY function newCircle = updateCircleGravity(circle, gravity) % Updates the magnitude of the x and y velocities of the circle % for friction. The drag force is equal to viscosity times % pi times radius times velocity. This drag force should % then be divided by the magnitude of the velocity to determine % a percent decrease for each of the x and y velocities. % USED BY: FRICTION function newCircle = updateCircleFriction(circle, viscosity) %Returns true if the edge of a circle is touching the bottom % edge of the board; false otherwise. % USED BY: SPLITTING function isAtBottomEdge = atBottomEdge(circle, board) % Processes the circles that need to be split. Checks if the board % allows splitting, and if so it checks each circle in circles to see % if that circle is atBottomEdge. If so, the circle needs to be split; % the new circle replaces the current one and the second new circle % is added to the end of the circle vector. The second new circle % is immediately drawn, and the new handle is added to the end of % the handle array. % % calls: atBottomEdge, drawCircle, splitCircle % USED BY: SPLITTING function [newCircles newHandles] = processCircleSplits(circles, handles, board) % Splits a circle into two circles. Each new circle should have radius % equal to 2 times radius divided by pi. Each new circle should % have velocity magnitude equal to half the magnitude of the original % circle. Direction for the first new circle should not be changed, % but the second new circle should have a random x and y velocity % (still with half the magnitude of the original circle velocity). % USED BY: SPLITTING function [newCircle1 newCircle2] = splitCircle(circle) % Removes small circles from the vector of circles. A small circle % is defined as any circle having a radius less than 1. Checks % if each circle has a radius greater than 1, and if so it places % the circle in the remainingCircles vector and its handle in the % remainingHandles vector. If not it deletes the corresponding handle. % % calls: delete (built-in) % USED BY: SPLITTING function [remainingCircles remainingHandles] = removeSmallCircles(circles, handles) % Processes collisions. Checks each circle with all of the other circles % to see if they overlap. If so, call collideCircles to determine the % new circles for both of the colliding circles. % % calls: checkCollision, collideCircles % USED BY: COLLISION function newCircles = processCollisions(circles) % Calculates new values for circle velocities after a collision % has been detected. % Steps: % 1. Calculate normal to the two circle centers % 2. Calculate unit normal/tangent. % 3. Calculate projection of both circle velocities onto normal % vector using dot product. % 4. Calculate projection of both circle velocities onto tangent % vector using dot product. % 5. Calculate the new velocity vector for both circles by swapping % the normal portions of the velocity. % 6. Set the x and y velocity fiels on the new circles. % USED BY: COLLISION function [newCircle1 newCircle2] = collideCircles(circle1, circle2) %Returns true if the current positions and radii of the circles cause % a collision (do they overlap?) or false if there is no collision. % % calls: getDistance % USED BY: COLLISION function isCollision = checkCollision(circle1, circle2) %Returns the distance between the centers of two circles. % USED BY: COLLISION function distance = getDistance(circle1, circle2)