lecture5-6

Sign23 count sumallsamesign end function z getzcross

This preview shows page 1. Sign up to view the full content.

This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: gles.txt % which contain the origin %load triangles.txt; %filename = input('name of triangles file: ','s'); triangles = load(filename); [rows cols] = size(triangles) a = triangles(:, 1:2); b = triangles(:, 3:4); c = triangles(:, 5:6); count = originInTriangle(a,b,c); fprintf('There are %5.0f triangles that enclose 0,0\n',count); end function count = originInTriangle( a,b,c ) %Checks to see if point p is contained in triangle a,b,c % a,b,c in xy plane z1 = getZcross(a,b); z2 = getZcross(b,c); z3 = getZcross(c,a); sign12 = sign(z1).*(sign(z1) == sign(z2)); sign23 = sign(z2).*(sign(z2) == sign(z3)); allSameSign = sign12.*sign23; count = sum(allSameSign); end function [ z ] = getZcross( a,b ) %getZcross returns the z-component of the cross product of % the vector a to b and b to 0,0 [m n] = size(a); zerocol = zeros(m,1); r = [b(:,1)-a(:,1) b(:,2)-a(:,2) zerocol]; s = [0 - b(:,1), 0 - b(:,2) zerocol]; v = crossProduct(r,s); z = v(:,3); end function [ xc ] = crossProduct( a,b ) %crossProduct returns the cross product components of a x b [m n] = size(a); xc = zeros(m,3); ax = a(:,1);...
View Full Document

Ask a homework question - tutors are online