CS3984 Computer Systems in Rust



Exercise 1

Note: This exercise is new! If there are any ambiguities in the specification or given materials, please don’t hesitate to ask for clarification either on the Discourse forum, via an email to Kieran, or during office hours.

Complete the exercise using rustlings, a framework for completing small Rust exercises.

  1. Install rustlings using the official installation instructions.
  2. Clone the exercise repository.
  3. Run rustlings in the cloned repository directory.

The assignment is comprised of 8 mini-exercises and 1 medium exercise. Complete the exercises by either making each exercise compile or by passing the tests included in the exercise.

Note: You are not allowed to modify any tests in the provided tests section.

Submission

Submission deadline: 11:59 pm, Sunday, September 15 2024.

Submission method: Run the included submit.py script in your root exercise repository. You will need Python and Requests installed. If you do not have Python and/or Requests, you can either:

  • Install Python and/or Requests
  • Copy your assignment solution (the exercises folder) and the submit script to rlogin and run the submit script there
    # Example:
    $ scp -r exercises/ submit.py \
      <VT_USERNAME_HERE>@rlogin.cs.vt.edu:cs3984-exercise-1
    
    
    

Part 1

The 8 mini-exercises (00_intro to 08_enums) are taken from the official rustlings exercise set.

Part 2

The medium exercise (99_shapes) requires incrementally implementing a Shape type, which provides a method to detect if two shapes are overlapping. The two shapes to implement are a Circle and a Rectangle. Each successive exercise file in 99_shapes builds upon the data structures implemented in the previous exercise file.

Complete the exercise by passing the tests provided in each exercise file. Study the tests to determine what fields and methods to implement, as well as the expected behavior of the method calls.

For q3_overlap, the algorithms to use for each method are given below in the formulas section.

Details

  1. The shapes live in an infinite grid with the following coordinate system:

    • The x-coordinate increases from left to right.
    • The y-coordinate increases from top to bottom.
  2. A Circle can be created from its center and radius.

  3. A Rectangle can be created from

    • the top-left corner (the corner of the rectangle with the smallest x and y coordinate), a width (in the positive x direction), and a height (in the positive y direction)

    • any two diagonally opposing corners

  4. A Rectangle should provide the following information:

    • x_min: the smallest x-coordinate located within the rectangle (or the x-coordinate of the left edge of the rectangle)
    • x_max: the largest x-coordinate located within the rectangle (or the x-coordinate of the right edge of the rectangle)
    • y_min: the smallest y-coordinate located within the rectangle (or the y-coordinate of the top edge of the rectangle)
    • y_max: the largest y-coordinate located within the rectangle (or the y-coordinate of the bottom edge of the rectangle)

Formulas

Circle-circle intersection formula for circles AA and BB: radiiSumSqAB=(Aradius+Bradius)2centerDiffAB=(AcxBcx)2+(AcyBcy)2OverlapAB=(centerDiffABradiiSumSqAB) \begin{align*} \text{radiiSumSq}_{AB} &= (A_{radius} + B_{radius})^2\\ \text{centerDiff}_{AB} &= (A_{cx} - B_{cx})^2 + (A_{cy} - B_{cy})^2\\ \text{Overlap}_{AB} &= (\text{centerDiff}_{AB} \le \text{radiiSumSq}_{AB}) \end{align*}

Rectangle-rectangle intersection formula for rectangles AA and BB: xOverlapAB=(Ax_min<Bx_max)(Ax_max>Bx_min)yOverlapAB=(Ay_max>By_min)(Ay_min<By_max)OverlapAB=xOverlapAByOverlapAB \begin{align*} \text{xOverlap}_{AB} &= (A_{x\_min} \lt B_{x\_max}) \land (A_{x\_max} \gt B_{x\_min})\\ \text{yOverlap}_{AB} &= (A_{y\_max} \gt B_{y\_min}) \land (A_{y\_min} \lt B_{y\_max})\\ \text{Overlap}_{AB} &= \text{xOverlap}_{AB} \land \text{yOverlap}_{AB} \end{align*}

Circle-rectangle intersection formula for circle CC and rectangle RR: closestXCR=clamp(Ccx,Rx_min,Rx_max)closestYCR=clamp(Ccy,Ry_min,Ry_max)distanceXCR=CcxclosestXCRdistanceYCR=CcyclosestYCRdistanceSqCR=distanceXCR2+distanceYCR2circleRadiusSqCR=Cradius2OverlapCR=distanceSq<circleRadiusSq \begin{align*} \text{closestX}_{CR} &= \text{clamp}(C_{cx}, R_{x\_min}, R_{x\_max})\\ \text{closestY}_{CR} &= \text{clamp}(C_{cy}, R_{y\_min}, R_{y\_max})\\ \text{distanceX}_{CR} &= C_{cx} - \text{closestX}_{CR}\\ \text{distanceY}_{CR} &= C_{cy} - \text{closestY}_{CR}\\ \text{distanceSq}_{CR} &= {\text{distanceX}_{CR}}^2 + {\text{distanceY}_{CR}}^2\\ \text{circleRadiusSq}_{CR} &= {C_{radius}}^2\\ \text{Overlap}_{CR} &= \text{distanceSq} \lt \text{circleRadiusSq} \end{align*}

Note: clamp(x,a,b)=max(a,min(x,b))\text{clamp}(x, a, b) = \text{max}(a, \text{min}(x, b))

Interactive Demo

Rectangle-Rectangle Intersection

Circle-Circle Intersection

Circle-Rectangle Intersection

Rectangle-Circle Intersection