Slides (PPTX)

Download Report

Transcript Slides (PPTX)

Week 4 - Friday



What did we talk about last time?
Data mining
Loops in Python



In Scratch there was a green block that allowed us to create random
numbers
In Python, there are functions that give us random values
Unfortunately, GlowScript only supports one of these methods,
random(), which gives a random floating point value between 0 and 1
value = random()

If you want a value between a and b, you can do the following:
value = (b – a)*random() + a

These are always floating point values, but you can use the int()
conversation operator to make integers
value = int((b – a)*random() + a)
Recall that we represent color as a group of red,
green, and blue intensities
 In Visual Python, those intensities are between 0 and 1

color1 = vector(1.0, 0, 0)
color2 = vector(1.0, 1.0, 1.0)
color3 = vector(0, 0, 0)

#bright red
#white
#black
We can create a sphere with a random color as follows
red = random()
green = random()
blue = random()
sphere(pos=vector(0,0,0), radius=1,
color=vector(red, green, blue))

Hardware refers to physical parts of the computer





Processor
Memory
Hard disk
Monitor
Software refers to the programs that run on it




Operating system (Windows, Mac OS, iOS, Linux, Unix)
Web browser (Internet Explorer, Safari, Firefox, Chrome)
Business applications (Word, PowerPoint)
Games





The “brains” of the computer
Fetches instructions and data from memory
Performs computations on the data based on
the instructions
Can send results to I/O
A modern CPU is made of
electronic circuitry
embedded in a small
silicon chip
How fast are computers?
I typed this PowerPoint on a computer running
at 2.4 GHz
 That’s 2,400,000,000 cycles per second
 Each cycle, your computer can do something
like:






Add
Subtract
Multiply
(Usually not divide)




“The density of transistors on a CPU doubles
every 18 months”
Historically, this has meant that CPU speeds
have doubled every 18 months
We can’t make things much faster because of
heat and power
We can still put more “stuff” into a CPU


Storage for all the data and instructions on
your computer
Modern computers store everything as binary
digits (bits) which have a value of 0 or 1.
1 byte
= 8 bits
1 kilobyte (kb)
= 210 bytes
1 megabyte (mb)
= 220 bytes
1 gigabyte (gb)
= 230 bytes
1 terabyte (tb)
= 240 bytes
Memory
What Can be Stored
Memory
1 byte
One character, like Q
4 gigabytes
A regular movie on DVD
A poem
8 gigabytes
An action movie on DVD
A short story
50 gigabytes A movie on Blu-ray
1 kilobyte
What Can be Stored
100 kilobytes
A low resolution photo
Two years of MP3 music
A novel
350,000 copies of War and Peace
1 terabyte
1 megabyte
A medium resolution photo
250 DVD quality movies
A minute of MP3 music
85,000 high resolution photos
Two CD quality albums
1 gigabyte
24 albums of MP3 music
Cache
• Actually on the CPU
• Fast and expensive
RAM
• Primary memory for a desktop computer
• Pretty fast and relatively expensive
Flash Drive
• Faster than hard drives
• Most commonly on USB keychains
Hard Drive
• Secondary memory for a desktop computer
• Slow and cheap
Optical Drive
• Secondary memory that can usually only be written once
• Very slow and very cheap
Monitor
• Common visual output device
Speakers
• Common audio output device
Mouse
• Common input device
Keyboard
• Common input device
Hard drives store data in magnetic patterns with a needle
hovering above magnetically coated platters that spin at
thousands of revolutions per minute
Platter
Pros
 Inexpensive
 Mature
technology
Cons
 Vulnerable to
 Impact
 Strong magnetic
fields


Noisy and prone
to failure
because of
moving parts
Generate heat
Needle
Flash drives store data in tiny charges in floating-gate transistors
embedded in silicon
Memory
Pros
 Fast
 No moving
parts
 No noise
 Very little heat
 Low power
Cons
 Expensive
 Limited number
of times it can
be written to
 But increasing!
Controller
 Plastic disc with patterns of dark, unreflective
spots (pits) for 0 and reflective spots (lands)
for 1 created on some reflective embedded
material, often aluminum
 As the disc spins, a laser shines on its surface
 Newer optical media uses shorter wavelength
light to get higher resolution
Pros
 Very cheap
 Easy to mass
produce
Cons
 Slow
 Many forms are
ROM
 Most others can
only be written
once
Land
Pit

A screen has both a width and a height in pixels
 The combination of these is called the resolution of the
screen

Higher numbers mean more pixels, allowing a more
accurate representation of images
Device
Resolution
iPhone 4
960 x 640
iPhone 6
1334 × 750
Older monitor
1024 x 768
720 HD TV
1280 x 720
1080 HD TV
1920 x 1080
4K monitor
3840 x 2160
One system for representing color is RGB
With Red, Green, and Blue components,
you can combine them to make most
visible colors
 Combining colors is an additive process:


 With no colors, the background is black
 Adding colors never makes a darker color
 Pure Red added to pure Green added to pure
Blue makes White


RGB is a good model for computer screens
CMYK is a subtractive system that is good
for printers (and for painting)
The design for a CCD is related to how
your eyes work
 The rod cells and cone cells in your eyes
are responsible for your sight
 Rods pick up faint light and are not
sensitive to different colors

 They are responsible for night and
peripheral vision

Cones are less sensitive but pick up
different colors
 There are L, M, and S cones that pick up
long, medium, and short wavelength light,
respectively
 L-cones pick up reddish light, M-cones pick
up greenish light, and S-cones pick up
bluish light
Photo credit:
https://www.flickr.com/photos/28931095@N03/

Offline rendering means that the rendering has
already happened when you see the images
 Used for television, movies, and print media
 Each frame produced by offline rendering often takes
hours to render

Real-time rendering is rendering done as you
watch it, typically in an interactive way
 Used mostly for video games
 Render rates are often between 20 and 60 frames per
second

For most offline and real-time rendered graphics, the
basic outline of producing images is the same
Modeling
Animation
Lighting
and Shading
Rendering
Modeling is creating the 3D objects
Animation is making them move
Lighting and shading determine the lighting of the
scene and other elements of visual appearance
 Rendering is the computation that determines the
final image




If you can't get an artist to
model the object for you,
there are a few other ways
 Generate the data procedurally
 Visualization of scientific (or
other) data as spheres, cubes,
or other primitives
 Sampling or scanning the real
world
 Reconstruction from
photographs
 Combinations!


Once you have the model, you have to make it
move around the scene
One part of this process is rigging, which ties
parts of the model together
 For example, pull the foot and it pulls the leg

The model can be moved to different key
frames
 Then a program can blend between them

Motion capture is also a popular method for
animating models
 The results can be more natural
1.
2.
3.
4.
5.
6.
7.
It's all just bits
Perfection is normal
There is want in the midst of plenty
Processing is power
More of the same can be a whole new thing
Nothing goes away
Bits move faster than thought




A computer represents all data with 1’s and
0’s
What does that all really mean?
Using semiconductor physics, we can make a
tiny little piece of a microchip be in one of
two states, say, OFF and ON, like a switch
If we say that OFF is 0 and ON is 1, then, by
using a lot of these switches, we can
represent a lot of 1’s and 0’s




The binary number system is base 2
This means that its digits are: 0 and 1
Base 2 means that you need 2 digits to
represent two, namely 1 and 0
Each place in the numeral as you move left
corresponds to an increase by a factor of 2
instead of 10


Each 1 in a base 2 numeral represents a
power of 2
Add up all those values




Find the largest power of 2 that is less than or
equal to the number
Subtract that power of 2 from the number
Repeat the process until you have zero
Every time you used a power of 2, write a 1 in
the representation of the number in base 2
 Otherwise, write a 0




Each character has a number
associated with it
These numbers can be listed
in tables
The ASCII table only covers 7
bits of information (0-127)
Unicode is a standard that
lets us represent characters
from all the world's
languages



To store a sound in a computer, we slice up a
wave and record the height of the wave as a
number
Each height value is called a sample
By getting 44,100 samples per second, we get a
pretty accurate picture of the wave



Abstraction may be the most important idea in
computer science
We take your problem and look at it until we can
see how it is similar to some other problem we
know how to solve
Details of how the computer works are hidden,
making it easier for the user
 But it can also lead to problems

An interface (also called an abstraction barrier)
is how you interact with a complex system, often
technology




A database is a collection of data and a set of
rules to organize the data by relationships
A database administrator makes the rules
and controls access
A database management system (DBMS) is
the program through which the user interacts
with the database
Oracle, Microsoft Access, Microsoft SQL
Server, MySQL, and PostgreSQL are popular
DBMSs


If we want to store data, we could use regular
files
But databases have many advantages:






Shared access for many users
Minimal redundancy so that space is used efficiently
Data integrity with rules that protect relationships
Controlled access with authorized users
Databases have also been heavily optimized for
speed
Users don’t need to know anything about the
actual physical layout of the database on disk

Almost all modern databases use the relational
database model
 The fundamental unit of organization is a table
 An older format for databases was hierarchical, like a
tree
A table consists of rows which are records
A record consists of fields or elements, which
are each a specific item of data
 A special field which is unique for each record is
called a primary key, which is used as an ID
number




A query is the name of a command given to a
database by a user
Queries can:





Retrieve
Modify
Add
Delete
Most databases allow commands to be issued
through a variant of SQL (Structured Query
Language)


Governments and companies collect so much
data that no person could analyze it
Data mining means looking for patterns in
massive amounts of data
 Cluster analysis: Find a group of records that are
probably related
▪ Like using cell phone records to find a group of drug dealers
 Anomaly detection: Find an unusual record
▪ Maybe someone who fits the profile of a serial killer
 Association rule mining: Find dependencies
▪ If people buy gin, they are also likely to buy tonic




Social media providers have access to lots of
data
Facebook alone has details about over a
billion people
Can they find hidden patterns about your life?
For research purposes, some sets of
"anonymized" data are made public
 But researchers often discover that the people
involved can be discovered anyway



Velocity gives the rate of change of location
over time
So, if you multiply a constant velocity by
time, you'll get the change in distance
In an equation, that's:
𝑙𝑜𝑐𝑎𝑡𝑖𝑜𝑛𝑓𝑖𝑛𝑎𝑙 = 𝑙𝑜𝑐𝑎𝑡𝑖𝑜𝑛𝑠𝑡𝑎𝑟𝑡 + 𝑣𝑒𝑙𝑜𝑐𝑖𝑡𝑦 × 𝑡𝑖𝑚𝑒
At a constant velocity,
you can simulate
movement on a
computer
 Take lots of little time
steps
 Multiply the velocity by
each time step to see
how far you've moved
 The result will be
accurate (up to the
rounding errors inside a
computer)

y
Time step
x




In real life, most things have some force
acting on them, causing some acceleration
Acceleration gives the rate of change of
velocity over time
So, if you multiply a constant acceleration by
time, you'll get the change in velocity
In an equation, that's:
𝑣𝑒𝑙𝑜𝑐𝑖𝑡𝑦𝑓𝑖𝑛𝑎𝑙 = 𝑣𝑒𝑙𝑜𝑐𝑖𝑡𝑦𝑠𝑡𝑎𝑟𝑡 + 𝑎𝑐𝑐𝑒𝑙𝑒𝑟𝑎𝑡𝑖𝑜𝑛 × 𝑡𝑖𝑚𝑒



We want to combine the two ideas together
If you're moving with a constant acceleration,
we can use a little calculus to find the total
distance traveled
The result is:
𝑙𝑜𝑐𝑎𝑡𝑖𝑜𝑛𝑓𝑖𝑛𝑎𝑙
= 𝑙𝑜𝑐𝑎𝑡𝑖𝑜𝑛𝑠𝑡𝑎𝑟𝑡 + 𝑣𝑒𝑙𝑜𝑐𝑖𝑡𝑦𝑠𝑡𝑎𝑟𝑡 × 𝑡𝑖𝑚𝑒
1
+ 𝑎𝑐𝑐𝑒𝑙𝑒𝑟𝑎𝑡𝑖𝑜𝑛 × 𝑡𝑖𝑚𝑒 2
2
y velocity
θ
𝑠𝑝𝑒𝑒𝑑 × sin 𝜃
x velocity
𝑠𝑝𝑒𝑒𝑑 × cos 𝜃

If you're moving at angle θ with respect to the
horizon
 The x component is 𝑠𝑝𝑒𝑒𝑑 × cos 𝜃
 The y component is 𝑠𝑝𝑒𝑒𝑑 × sin 𝜃

If you want to find the total speed, use the
Pythagorean theorem:
2
 𝑠𝑝𝑒𝑒𝑑 2 = 𝑠𝑝𝑒𝑒𝑑𝑥 + 𝑠𝑝𝑒𝑒𝑑𝑦

2
If you want to find the direction, you can use
trigonometry again:
 tan 𝜃 =
𝑠𝑝𝑒𝑒𝑑𝑦
𝑠𝑝𝑒𝑒𝑑𝑦
 𝜃 = arctan
𝑠𝑝𝑒𝑒𝑑𝑦
𝑠𝑝𝑒𝑒𝑑𝑦



The first program in many programming
languages is called Hello, world
It's a test to see if text input and output are
working
In Python, you can type the following to write
the Hello, world program
print("Hello, world!")

It will print Hello, world! on the next line
In Scratch, we can define a variable by going to the orange
Data section and clicking the Make a Variable button
 In Python, we assign a value to a name and that's it

person = "Walter White"
The = sign is the assignment operator
Think of it as an arrow that points left, storing whatever is
on the right into the variable
 We can use whatever is stored in a variable like it's a value


print(person)



Visual Python is an extension to Python that lets
us easily manipulate 3D objects
I chose Visual Python because it's easier to learn
program when you can see the effects of your
code
Any Visual Python program should have the
following line at the top of it:
from visual import *

However, GlowScript.org includes this
automatically, so it won’t be necessary for us

The box() command will make a
box
 pos, size, color are attributes

The sphere() command will make
a sphere
 pos, radius, and color are
attributes
redbox=box(pos=vector(4,2,3),
size=vector(8,4,6),color=color.red)
ball=sphere(pos=vector(4,7,3),radius=2,
color=color.green)


Good news: There are only two loops to learn in
Python
The while loop runs as long as a condition is
true
countdown = 10
while countdown > 0:
print(countdown)
countdown = countdown – 1
print("Blast-off!")

In this case, when countdown becomes 0, the
loop stops (since 0 is not less than 0)


If you want to do something, say, 10 times in
Python, you can use a for loop
But you need to call the range() method to
generate a list of numbers for you
for i in range(10):
print(i)

In this case, the loop will run 10 times, and
the numbers 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9
will print out



The first line of every loop ends with a colon (:)
If you don't put it there, Python will not run your
code
The stuff inside of a loop needs to be indented
with a tab
 That's how you know what should be repeated and
what is after the loop
tab

for i in range(10):
print(i)
colon
If you type the colon, Python automatically
indents the next line for you

Exam 1!




Turn in Project 1 by 11:59 tonight
Review Python Chapters 1 – 3
Review Blown to Bits Chapter 1
Study for Exam 1 next Monday