0% found this document useful (0 votes)
14 views

Assignment 1 Matlab (PDF) - Course Sidekick

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Assignment 1 Matlab (PDF) - Course Sidekick

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Assignment 1 Matlab .

pdf

Helpful Unhelpful

Home / Mathematics

Guide to MATLAB Assignment 1: A Clipping Triangle


Updated 9/1/23

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

• -2 if the equation has no solutions (e.g. ),


• -1 if the equation is solved by all values of x and y ( ),
• 0 if the solutions of do not intersect T (e.g. ),
• 1 if the solutions of intersect T in one point (e.g. ),
• 2 if the solutions of intersect T in more than one point (e.g. ).

The variable list, which reports the distinct endpoints of the intersection, should be a

• 2x0 empty array (empty list of 2-vectors) if count is -2, -1 or 0,


• 2x1 array (a single column vector)-- the point of unique intersection--if count is 1,
• 2x2 array (list of two column vectors)--the endpoints of the intersection-- if count is 2.

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

if c == 0 % solution set is the plane

count = -1; % special return value signaling the plane

else % solution set is empty

count = -2; % special return value signaling the empty set

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 .

if a==0 && b~=0 % horizontal line (0x+by=c)

y = c/b; % y coordinate of the line 0x+by=c

if 0<=y % line is not below T

if y<1 % infinite intersection


elseif
list
count==y==1
[0
2;1-y;
% y
singleton
y]; % (0,y)
intersection
and (1-y,y)

count = 1;

list = [0; 1]; % (0,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

% Insert code needed to handle the case of a 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

• increment (add 1 to) count and


• add the coordinates of the intersection to list.

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

% check intersection with the y-axis (x=0)

y = c/b; % y coordinate of the intersection

if 0<=y && y<1 % is the intersection on the left edge?

count = count + 1; % endpoint count increased by 1

list(:,count) = [0; y]; % the endpoint (0,y) added to the list

end

% check intersection with the x-axis (y=0)

%%%% Code to process the intersection with the x-axis is needed here.

% check intersection with the hypotenuse (x+y=1)

%%%% 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

plot([0 0 1 0],[0 1 0 0])

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')

which will hopefully produce a figure something like this:

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

Accounting Aerospace Engineering

Anatomy Anthropology

Arts & Humanities Astronomy

Biology Business

Chemistry Civil Engineering

Computer Science Communications

Economics Electrical Engineering

English Finance

Geography Geology

Health Science History

Industrial Engineering Information Systems

Law Linguistics

Management Marketing

Material Science Mathematics

Mechanical Engineering Medicine

Nursing Philosophy

Physics Political Science

Psychology Religion
Sociology Statistics

LEGAL COMPANY
Copyright Policy Documents Sitemap

Honor Code Study Guides

Terms

Academic Integrity

Cookie Policy

Privacy Policy

Do not Sell or Share My Personal Info

CONNECT WITH US
Facebook

Instagram

YouTube

Twitter

© Learneo, Inc. 2024


Course Sidekick is not sponsored or endorsed by any college or university.

You might also like