Python Programming: An Introduction to Computer Science, 3rd Ed.
Python Programming: An Introduction to Computer Science, 3rd Ed.
3rd Edition
ISBN: 9781590282755
Author: John Zelle
Publisher: Franklin, Beedle & Associates
bartleby

Videos

Question
Book Icon
Chapter 8, Problem 13PE
Program Plan Intro

Regression Line

Program plan:

  • Import the header file.
  • Define the “graphWin” method
    • Create a window
    • Set the coords
    • Display the message on the window
    • Call the “draw” method for set the point
    • Call the “Line” method for draw the line
    • Call the “draw” method for set the point
    • Call the “Line” method for draw the line
    • Call the “draw” method
    • Call the “Rectangle” method
    • Get the center position
    • Set the “Done” text
    • Call the “draw” method stop the program after clicking the “Done” text
    • Accept the input from the user
    • Set the list
    • Make the condition true
      • Get the mouse action
      • Check the condition
        • Break the loop
      • Otherwise, store user points in an appended list
      • Call the “draw” method
    • Return the result
  • Define the “average” method
    • Set the values for the variables
    • Iterate “i” until it reaches “allPoints” value
      • Get the “x” and “y” value
      • Calculate “sumX”, and “sumY”
      • Increment the “count” value
      • Calculate the “sumXiYi, “sqX”, “sumSqXi”, “SqY”, and sumSqYi” values
    • Calculate the “a” and “b” values
    • Return the values
  • Define “main” method
    • Display the text on the window
    • Call the “average” method
    • Set the values
    • Call the “Line” method
    • Call the “draw” method
    • Get the mouse action
  • Call the function “main()”.

Expert Solution & Answer
Check Mark

Explanation of Solution

Program:

#import the required header files

from graphics import *

import math as m

#definition of "graphWin" method

def graphWin(Title):

    #Create a graphics window

    win = GraphWin(Title, 400, 400)

    #set the coords

    win.setCoords(-10, -10, 10, 10)

    #tell user to select multiple locations on the screen to designate points

    message = Text(Point(-3, 8), "Click to delineate points on the graph.")

    #draw the point

    message.draw(win)

    #call the "Line" method

    axisX = Line(Point(-10,0), Point(10,0))

    #call the "draw" method

    axisX.draw(win)

    #call the "Line" method

    axisY = Line(Point(0,10), Point(0,-10))

    #call the "draw" method

    axisY.draw(win)

    #call the "Rectangle" method

    r = Rectangle(Point(-9, -9), Point(-7,-8))

    #call the "draw" method

    r.draw(win)

    #get the centre position

    rCenter = r.getCenter()

    #set the text

    stopMouse = Text(rCenter, "Done")

    #call the "draw" method

    stopMouse.draw(win)

    #accept input from user until <done> button is pressed

    click = Point(0,0)

    #set the list

    allPoints = []

    #check the condition

    while True:

        #get the mouse action

        click = win.getMouse()

        #check the condition

        if ((-9 <= click.getX() <= -7) and (-9 <= click.getY() <= -8)):

            #break the loop

            break

        #otherwise

        else:

            #store user points in an appended list

            allPoints.append(click)

            #call the "draw" method

            click.draw(win)

    #return the values

    return allPoints, win

#definition of "average" method

def average(allPoints):

    #set the values for the variables

    sumX = 0

    sumY = 0

    count = 0

    sumXiYi = 0

    sumSqXi = 0

    sumSqYi = 0

    #iterate "i" until it reaches "allPoints" value

    for i in allPoints:

        #get the "x" and "y" value

        x = i.getX()

        y = i.getY()

        #calculate the "sumX" value

        sumX = sumX + x

        #calculate the "sumY" value

        sumY = sumY + y

        #increment the value

        count = count + 1

        #calculate the "xy" value

        xy = x * y

        #calculate the "sumXiYi" value

        sumXiYi = sumXiYi + xy

        #calculate the "SqX" value

        SqX = x * x

        #calculate the "sumSqXi" value

        sumSqXi = sumSqXi + SqX

        #calculate the "SqY" value

        SqY = y * y

        #calculate the "sumSqYi" value

        sumSqYi = sumSqYi + SqY

    #calculate the "a" and "b" value

    a = ((sumY * sumSqXi) - (sumX * sumXiYi)) / (count * (sumSqXi) - sumX ** 2)

    b = ((count * sumXiYi) - (sumX * sumY)) / (count * (sumSqXi) - sumX ** 2)

    #return the values

    return a, b

#definition of "main" method

def main():

    #get the text for the window

    allPoints, win = graphWin("Regression Line")

    #call the method

    a, b = average(allPoints)

    #set the values

    x1 = -10

    x2 = 10

    #call the "Line" method

    regressLine = Line(Point(x1, (a + b * x1)), Point(x2, (a + b * x2)))

    #call the "draw" method

    regressLine.draw(win)

    #get the mouse action

    win.getMouse()

#call the "main" method

main()

Sample Output

Output:

Screenshot of “Regression Line” window

Python Programming: An Introduction to Computer Science, 3rd Ed., Chapter 8, Problem 13PE , additional homework tip  1

After clicking “Done” button, the below window will appear

Screenshot of “Regression Line” window

Python Programming: An Introduction to Computer Science, 3rd Ed., Chapter 8, Problem 13PE , additional homework tip  2

Want to see more full solutions like this?

Subscribe now to access step-by-step solutions to millions of textbook problems written by subject matter experts!
Students have asked these similar questions
Write the following function that draws a line from point (x1, y1) to (x2, y2) with color (default to black) and line size (default to 1).def drawLine(x1, y1, x2, y2, color = "black", size = 1):
You are to write a program that allows the user to draw a simple house using 5 mouse clicks. - The first two clicks will be the opposite corners of the rectangular frame of the house. - The third click will indicate the center of the top edge of the rectangular door. The door should have a total width that is 1/5 of the width of the house frame. The sides of the door should extend from the corners of the top down to the bottom of the frame. - The 4th click will indicate the center of a square window. The window is as wide as the door. (A change from the original book program.) - The last click will indicate the peak of the roof. The edges of the roof will extend from the point at the peak to the corners of the top edge of the house frame. Make the lines of the roof edge green. (Another change from the original book program.)
An archery target consists of a central circle of yellow surrounded by con-centric rings of red, blue, black and white. Each ring has the same width, which is the same as the radius of the yellow circle. Write a programthat draws such a target. Hint: Objects drawn later will appear on top ofobjects drawn earlier.
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education
Java random numbers; Author: Bro code;https://www.youtube.com/watch?v=VMZLPl16P5c;License: Standard YouTube License, CC-BY