Assignment 1 Matlab (PDF) - Course Sidekick
Assignment 1 Matlab (PDF) - Course Sidekick
Helpful Unhelpful
Home / Mathematics
The problem considered here is to determine the intersection of a line, defined by a linear equation ,
with the clipping tringle T having vertices (0,0), (1,0) and (0,1). This intersection, typically a line segment, is
defined by its endpoints. Owing to the many different situations that arise depending on the linear equation,
there is some inherent complexity that requires the consideration of various special cases. Here we use
the geometry of the problem to guide us in sorting through the cases. We then translate our geometric
understanding into analytic formulas understood by MATLAB. Later when we study Gaussian elimination,
we encounter many of the same complexities but will be without a robust geometric intuition due to high
dimensionality. The moral of the story is that we need to both make use of our geometric understanding, when
available, and learn to use purely analytic methods when necessary.
Problem Statement: Create a function named tclip that determines the intersection of a line (solution of a
given linear equation) with a clipping triangle T having vertices (0,0), (1,0) and (0,1). The function declaration
should have the form
function [count, list] = tclip(a,b,c)
where the line in question is ordinarily the solution set of the linear equation . The value of the
variable count returned should be
The variable list, which reports the distinct endpoints of the intersection, should be a
Solution template: In this assignment you are given an extensive template consisting of code that handles a
number of special cases. You will need to add code to the template to handle the remaining cases. To make
this addition you should understand the logic of the given code which is discussed below.
G tti t t d T t t f ll th f ti d l ti ith th d f lt i t f th t t i bl
Getting
function started:
intersections To start,
[count, list] we follow thethese
function
= tclip(a,b,c) declaration with the default assignment of the output variables,
count = 0;are later discovered, then
% default values
return will be overwritten
value with the
(no endpoints) appropriate results.
count and list, assuming that no intersections between the line and T are found. If nonempty
list = zeros(2,0); % default return value (empty list of endpoints)
Page 1 of 7
General Idea: The general idea that we pursue here is to consider the intersection of the line defined by the
equation with each of the three lines (x-axis), (y-axis), and , which bound T,
and determine if those intersections fall on an edge of T. Each of these lines is generally expected to intersect
the line at a single point, but there are some special cases that must be considered to completely
solve the problem:
Case 0 (no line): The solution set of the equation is a line except when and . In this
case, there are two subcases depending on c. If then no ordered pair can satisfy the equation; that
is, the solution set is empty. If then the solution set will consist of every ordered pair , that is, of every
point in the plane.
if a==0 && b==0 % solution set of linear equation not a line
end
return % ends the function (not further function code will be executed)
end
Note that the execution of the return statement terminates the function so no further code from the function
will be executed.
The next three cases correspond to lines that are parallel to one of the edges of T . Such lines will fail to have
unique intersections with those sides (either failing to intersect or intersecting every point in the edge).
Case 1 (horizontal line): The equation represents a horizontal line if and . The
y-coordinate of this line will be . If this coordinate is between 0 and 1then the line intersects the left edge
of T at and the hypotenuse of T at . Note that there are two subcases: the intersection is a line
segment of infinitely many points if but a single point if .
count = 1;
end
end
return
end
Note that the endpoints of the intersection are stored in list are column vectors; that is, the x-coordinates are
on the first row and the y-coordinates are on the second row. Also note that if no intersection is found the
return values were set at the beginning of the function.
Case 2 (vertical line): The equation represents a vertical line if and . The x-coordinate
of this line will be . If this coordinate is between 0 and 1 then the line intersects the bottom edge of T
at and the hypotenuse of T at . Note that there are two subcases: the intersection is a line
segment of infinitely many points if but a single point if . You are expected to provide the
code to treat this case.
if a~=0 && b==0 % vertical line
return
end
end
3
Case 3 (line parallel to hypotenuse): The equation represents a line parallel to the hypotenuse of
the clipping triangle if . This line will intersect the x-axis at . If this coordinate is between 0 and
then the line intersects the bottom edge of T at and the left edge of T at . Note that there are two
subcases the intersection is a line segment of infinitely many points if but a single point if .
You are expected to provide the code to treat this case.
if a-b == 0 && a~=0 % line parallel to the hypotenuse
% Insert code needed to handle the case of a line parallel to the hypotenuse
return
end
4
Case 4 (generic line): The equation represents a line that is not parallel to any edge of T ; i.e., ,
and . This line will have a unique intersection with each of the lines that contains edges of T.
The strategy used here is to find those intersections and determine if that intersection lies on an edge of T.
One slightly subtle aspect of this process is that to identify endpoints unique we assign each vertex of T to be
part of exactly one edge; otherwise, a vertex would be identified (and counted) twice as an endpoint. Thus,
we assign to be part of the left edge, to be part of the bottom edge and to be part of the
hypotenuse. We check each edge for an intersection. When an intersection is found we
The intersection with the y-axis occurs at . If then the intersection point is on the left edge.
The intersection with the x-axis occurs at . If then the intersection point is on the bottom
edge.
The intersection with the line containing the hypotenuse is found solving the system
The intersection has y-component and x-component . The intersection lies on the
hypotenuse if .
if a~=0 && b~=0 && a-b~=0
end
%%%% Code to process the intersection with the x-axis is needed here.
%%%% Code to process the intersection with the hypotenuse is needed here.
return
end
5
Testing, testing, testing: You may find it most convenient to first develop the function tclip in MATLAB as
an m-file and then paste this working function into the MATLAB grader assignment once you have it working
perfectly. You will of course want to copy the template from the assignment to begin the process. Once you
have working code you might want to test it using a testing script such as
clf
hold on
for a=0:5
for b=-5:5
for c=-10:10
[a b c]
[count, list]=tclip(a,b,c)
if count == 2
plot(list(1,:),list(2,:))
plot(list(1,:),list(2,:),'g.','MarkerSize',10)
elseif count == 1
plot(list(1,1),list(2,1),'r.','MarkerSize',20)
end
end
end
6
end
print('ctf1','-djpeg')
The visual display of the computed intersections for a large number of lines, including many special cases,
generally makes detecting coding errors easier.
7
Uploaded by PrivateFog4160 on coursehero.com
Section 4.4.3
ENGsoSurvey
Section 5.2.7
COMS143Handout1[1]
COMS143-Demonstration-1[1]
Section 5.2.8
MATH-9446-Study Encyclopedias
Screenshot (309)
Math1090-Final Review-Worksheet
Section 4.2.4
CSECMaths-Paper2-July2021 Solutions
Section 4.2.5
SUBJECTS
Anatomy Anthropology
Biology Business
English Finance
Geography Geology
Law Linguistics
Management Marketing
Nursing Philosophy
Psychology Religion
Sociology Statistics
LEGAL COMPANY
Copyright Policy Documents Sitemap
Terms
Academic Integrity
Cookie Policy
Privacy Policy
CONNECT WITH US
Facebook
YouTube