L17. Structures Simple Structures Structure Arrays Structures with Array Fields Other Possibilities.

Slides:



Advertisements
Similar presentations
Insight Through Computing 13. More on Arrays Square Bracket Notation Subscripts Plotting and color Built-In Functions: ginput, fill, sum, axis.
Advertisements

CIRCLES Unit 3-2. Equations we’ll need: Distance formula Midpoint formula.
Insight Through Computing 17. Structures Simple Structures Structure Arrays Structures with Array Fields Other Possibilities.
Distance and Midpoint Formulas; Circles
9.1 – 9.2 Exploring Circles Geometry.
Chapter 1 Functions and Graphs Copyright © 2014, 2010, 2007 Pearson Education, Inc Distance and Midpoint Formulas; Circles.
10.7 Write and Graph Equations of Circles Hubarth Geometry.
Circles: Objectives/Assignment
10.6 Equations of Circles Advanced Geometry. What do you need to find the equation of a circle? Coordinates of the Center of the circle. Radius – Distance.
Chapter 1.7 Midpoint and Distance in a Coordinate Plane
(x 1, y 1 ) (x 2, y 2 (x 1, y 1 ) (x 2, y 2 ) |x 1 – x 2 | |y 1 – y 2 | d.
1-3 The Distance and Midpoint Formulas
© William James Calhoun, : Midpoint of a Line Segment OBJECTIVE: You will find the coordinates of the midpoint of a line segment in the coordinate.
Dr. S.M. Malaek Assistant: M. Younesi
Distance and Midpoints
Use Midpoint and Distance Formulas
Slide Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Distance and Midpoint Graphing, Symmetry, Circles Solving.
Warm Up 1. Graph A (–2, 3) and B (1, 0). 2. Find CD. 8
Midpoint and Distance Formulas Goal 1 Find the Midpoint of a Segment Goal 2 Find the Distance Between Two Points on a Coordinate Plane 12.6.
{ Chapter 1: Functions and their Graphs 1.1 Rectangular Coordinates and 1.2 Graphs of Equations.
The Distance and Midpoint Formulas! To be used when you want to find the distance between two points or the midpoint between two points.
1 © 2010 Pearson Education, Inc. All rights reserved © 2010 Pearson Education, Inc. All rights reserved Chapter 2 Graphs and Functions.
coordinates, lines and increment
DISTANCE AND MIDPOINT. DISTANCE  Given any two points on a coordinate plane you can find the distance between the two points using the distance formula.
Midpoint and Distance Formulas Section 1.3. Definition O The midpoint of a segment is the point that divides the segment into two congruent segments.
HAWKES LEARNING SYSTEMS math courseware specialists Copyright © 2011 Hawkes Learning Systems. All rights reserved. Hawkes Learning Systems: College Algebra.
The Distance and Midpoint Formulas
1.5 – Midpoint. Midpoint: Point in the middle of a segment A B M.
Chapter 2 Functions and Graphs Copyright © 2014, 2010, 2007 Pearson Education, Inc Distance and Midpoint Formulas; Circles.
Precalculus Fifth Edition Mathematics for Calculus James Stewart Lothar Redlin Saleem Watson.
Unit 1 – Conic Sections Section 1.2 – The Circle Calculator Required.
Slide Copyright © 2006 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Precalculus Parametric Equations graphs. Parametric Equations  Graph parametric equations.  Determine an equivalent rectangular equation for parametric.
Section 1.1 Introduction to Graphing Copyright ©2013, 2009, 2006, 2001 Pearson Education, Inc.
Topic 5-1 Midpoint and Distance in the Coordinate plan.
1-6 Midpoint and distance in the coordinate plane
12.4 The Distance Formula Objectives: Use the distance formula to find the distance between 2 points in a coordinate plane. Determine whether a triangle.
Chapter 1 Functions and Graphs Copyright © 2014, 2010, 2007 Pearson Education, Inc Distance and Midpoint Formulas; Circles.
1.9 Distance & Midpoint Formulas Circles Objectives –Find the distance between two points. –Find the midpoint of a line segment. –Write the standard form.
1.1 and 1.5 Rectangular Coordinates and Circles Every man dies, not every man really lives. -William Wallace.
Holt Geometry 1-6 Midpoint and Distance in the Coordinate Plane Happy Monday!!! Please take out your assignment from Friday and be ready to turn it in.
A relation is a correspondence between two sets. If x and y are two elements in these sets and if a relation exists between x and y, then x corresponds.
Geometry Sections 5.1 and 5.2 Midsegment Theorem Use Perpendicular Bisectors.
LESSON 10-4 Equations of Circles
Circles Learning goals: Write the equation of a circle. Use the equation of a circle and its graph to solve problems. Graphing a circle using its four.
9.6 Circles in the Coordinate Plane Date: ____________.
1 The Coordinate Plane Just as points on a line can be identified with real numbers to form the coordinate line, points in a plane can be identified with.
 Then: You graphed points on the coordinate plane.  Now: 1. Find the distance between points. 2. Find the midpoint of a segment.
1 Lesson 1-3 Use Midpoint and Distance Formula. Warm Up 2 1.Find a point between A(-3,5) and B(7,5). 2.Find the average of -11 and 5. 3.Solve 4.Find 
Sec 1.8 Circles Objectives: To understand the distance and midpoint formulas. To understand the equations of circles and their graphs.
1.6: The Midpoint Formula Objective:
Computer Graphics CC416 Lecture 04: Bresenham Line Algorithm & Mid-point circle algorithm Dr. Manal Helal – Fall 2014.
Section 2.8 Distance and Midpoint Formulas; Circles.
Midpoint and Distance Formulas
Midpoint and Distance Formulas
Copyright © 2014, 2010, 2007 Pearson Education, Inc.
Distance and Midpoints
Resolving Vectors into Components – the “Vector Collector”
COORDINATE PLANE.
Introduction to Graphing
1-6 Midpoint & Distance in the Coordinate Plane
Congruent segments MIDPOINT OF A SEGMENT
Geometry Unit 12 Distance and Circles.
EQ: What is the midpoint formula?
L13. More on Arrays Square Bracket Notation Subscripts
Objective: To calculate midpoint and distance in the xy-plane.
Midpoint in the Coordinate Plane
Outline Collect syllabi Go over daily quiz
Midpoints and Distance
Presentation transcript:

L17. Structures Simple Structures Structure Arrays Structures with Array Fields Other Possibilities

Data is Often Related A point in the plane has an x coordinate and y coordinate. If a program manipulates lots of points, there will be lots of x’s and y’s. Anticipate clutter. Is there a way to “package” the two coordinate values?

Packaging Affects Thinking Our Reasoning Level: P and Q are points. Compute the midpoint M of the connecting line segment. Behind the scenes we do this: M x = (P x + Q x )/2 M y = (P y + Q y )/2

Seen This Before Functions are used to ``package’’ calculations. Elevates the level of our reasoning. Critical for problem solving.

Simple Example P1 = struct(‘x’,3,’y’,4); P2 = struct(‘x’,-1,’y’,7); D = sqrt((p1.x-p2.x)^2 + (p1.y-p2.y)^2); Distance between two points. p1.x, p1.y, p2.x,p2.y participating as variables—because they are.

Initialization P1 = struct(‘x’,3,’y’,4); p1 is a structure. The structure has two fields. Their names are x and y. They are assigned the values 3 and 4.

How to Visualize p1 p1 xy 3 4 P1 = struct(‘x’,3,’y’,4);

Accessing a Field p1 xy 3 4 A = p1.x + p1.y Assigns the value 7 to A.

Assigning to a Field p1 xy 3 4 p1.x = p.y^2 Will assign the value 16 to p1.x

Another Example A = struct(‘name’,’New York’,… ‘capital’, ‘Albany’,… ‘Pop’,15.5) Can have combinations of string fields and numeric fields.

Legal/Illegal Maneuvers P = struct(‘x’,3,’y’,4) Q = struct(‘x’,5,’y’,6) R = Q % Legal. R is % copy of Q S = (Q+R)/2 % Illegal.

Legal/Illegal Maneuvers % Illegal… P = struct(‘x’,3,’y’) P.y = 4 % Legal P = struct(‘x’,3,’y’,[]) P.y = 4 Using the Empty array as a place holder

Functions and Structures function d = dist(P,Q) % P and Q are points. % d is the distance between them D = sqrt((P.x-Q.x)^2 + … (P.y-Q.y)^2);

Sample “Make” Function function P = MakePoint(x,y) % P is a point with P.x and P.y % assigned the values x and y. P = struct('x',x,'y',y); Good Style. Highlights the structure’s definition.

Functions and Structures function DrawLS(P,Q,c) % P and Q are points. % Draws a line segment connecting % P and Q. Color specified by c plot([P.x Q.x],[P.y Q.y],c)

Pick Up Sticks Script s = 'rgbmcy'; for k=1:100 P = MakePoint(randn(1),randn(1)); Q = MakePoint(randn(1),randn(1)); c = s(ceil(6*rand(1))); DrawLS(P,Q,c) end Generates two random points and chooses one of six colors randomly.

Structure Arrays An array whose components are structures. And all the structures are the same. Example: A point array…

P(1) xy.50 An Array of Points.86 P(1) = MakePoint(.50,.86)

P(2) xy -.50 An Array of Points.86 P(2) = MakePoint(-.50,.86)

P(3) xy An Array of Points 0.0 P(3) = MakePoint(-1.0,0.0)

P(4) xy -.5 An Array of Points -.86 P(4) = MakePoint(-.50,-.86)

P(5) xy.5 An Array of Points -.86 P(5) = MakePoint(.50,-.86)

P(6) xy 1.0 An Array of Points 0.0 P(6) = MakePoint(1.0,0.0)

An Array of Points function P = CirclePoints(n) theta = 2*pi/n; for k=1:n c = cos(theta*k); s = sin(theta*k); P(k) = MakePoint(c,s); end

Place n points uniformly around the unit circle. Draw all possible triangles obtained by connecting these points 3-at-a-time. Problem

function DrawTriangle(P,Q,R,c) % P, Q, and R are points and c % is a color, e.g., 'w','r','c', etc % Draws a colored triangle with those vertices. fill([P.x Q.x R.x P.x],… [P.y Q.y R.y P.y],c) Will Need This…

These triangles are the same: ( 1,4,6), (1,6,4), (4,1,6), (4,6,1), (6,1,4), (6,4,1)

No! for i=1:n for j=1:n for k=1:n Draw a triangle with Vertices P(i), P(j), and P(k) End end i, j, and k should be different

Question Time What is the 7 th line of output: for i=1:5 for j=i+1:5 x = 10*i + j end A. 7 B. 21 C. 22 D. 23 E. Other

for i=1:n for j=i+1:n for k=j+1:n disp([i j k]) end All Possible (i,j,k) with i < j < k

i = 1 i = 4 i = 3 i = 2

for i=1:n for j=i+1:n for k=j+1:n DrawTriangle( P(i),P(j),P(k),'m') DrawPoints(P,'w') pause DrawTriangle(P(i),P(j),P(k),'k') end Triangle Solution!

Structures with Array Fields Let’s develop a structure that can be used to represent a colored disk. Four fields: xc: x-coordinate of center yc: y-coordinate of center r: radius c: rgb color vector

Examples D1 = struct(‘xc’,1,’yc’,2,’r’,3,… ’c’,[1 0 1]) D2 = struct(‘xc’,4,’yc’,0,’r’,1,… ’c’,[.2.5.3])

Problem Lets compute the “average” of D1 and D2: r = (D1.r + D2.r) /2; xc = (D1.xc + D2.xc)/2 yc = (D1.yc + D2.yc)/2 c = (D1.c + D2.c) /2; D = struct(‘xc’,xc,’yc’yc,’r’,r,’c’,c)

Example D

A Structure Can Have a Field That is a Structure A = MakePoint(2,3) B = MakePoint(4,5) L = struct(‘P’,A,’Q’,B) (One way to represent a line Segment with endpoints P and Q.) S = L.P.y assigns 3 to S.

A Structure Array with Components Whose Fields Are Arrays function P = MakeScore(s,n) T = ceil(6*rand(1,n)); P = struct(‘name’,s,’throws’,T)

Example D1 D2

G(1) = MakeScore(‘Me’,10) G(2) = MakeScore(‘You’,10) for k=1:10 if G(1).throws(k) > G(2).throws(k) disp(‘I win’) end

Hierarchy G a structure array G(1) component in the structure array G(1).throws a field in the component G(1).throws(3) a component of the field