• Home
  • Textbooks
  • You Can Do It!: A Beginners Introduction to Computer Programming
  • You CanUsepoint2d

You Can Do It!: A Beginners Introduction to Computer Programming

Francis Glassborow

Chapter 6

You CanUsepoint2d - all with Video Answers

Educators


Chapter Questions

06:09

Problem 1

A triangle is a shape with exactly three vertices. There are several formulae for calculating the area of a triangle. One of them is based on the lengths of the three sides. Conventionally the lengths of the sides of a triangle are represented by a, b and c. Using s to represent $(a+b+c) / 2$, the area of a triangle is given by the square root of $s^*(s-a)^*(s-b)^*(s-c)$. (I have used the computing symbols for multiplication and division to help non-mathematical readers.)
Use the above information to write a function to calculate the area of a triangle.

Willis James
Willis James
Numerade Educator
00:25

Problem 2

Any polygon can be decomposed into $(n-2)$ triangles where $n$ is the number of vertices. The simplest way to do this is to choose a vertex and join it to each of the other vertices that are not adjacent to it. The following diagram illustrates that process for a pentagon and a hexagon:

In the above cases the area of the whole polygon is the result of adding together the triangles of which they are composed. Unfortunately this is not always the case, as you will see if you use the following shape and the point indicated: For the purposes of this exercise you can assume that you do not have such a case.

Write a function that calculates the area of a polygon by summing the areas of the triangles that are obtained by joining the first vertex of a shape object to each of the others.

I am not providing a model answer to this one because I do not want to place temptation in your way. It you cannot manage it now, come back to it later.

Ian Shi
Ian Shi
Numerade Educator
03:00

Problem 3

Those with a fair knowledge of transformation geometry may be familiar with transformation matrices. If you are, try to write a function that applies the concept of a transformation matrix

Emily Min
Emily Min
Numerade Educator
02:37

Problem 4

Given the three vertices of a triangle you can compute a pair of mathematical (3-dimensional) vectors that together with a single vertex represent the triangle. As we are working in two dimensions, the vector cross product will give a value in the third (unused) dimension whose absolute value is twice the area of the triangle.
Write a function that will determine the area of a triangle using a vector cross product.

Do not discard the sign of the result.
Now use this function to calculate the area of a shape object assuming that the vertices define a shape whose sides do not intersect.
My solution is provided at the end of the chapter.

Khoobchandra Agrawal
Khoobchandra Agrawal
Numerade Educator
00:32

Problem 5

The problem with our shape objects is that some orderings of the vertices will give polygons (without intersecting sides) and some will not. Your task is to write a function that will determine whether a specific shape object is a polygon with non-intersecting sides.

Please note that, in general, there is more than one ordering of vertices that produces a polygon so it is not possible to write a function that re-orders the vertices in a way that provides a unique polygon.

This problem has largely been included to highlight the difference between domain knowledge, programming knowledge and programming skill. At this stage you have all the programming knowledge required to complete this exercise. If you also have adequate domain knowledge, the only thing holding you back is programming skill. Without the domain knowledge you cannot complete this exercise.

Katelyn Chen
Katelyn Chen
Numerade Educator