Question

In: Computer Science

The columns of the input file correspond to the point coordinates, for example, the first column...

The columns of the input file correspond to the point coordinates, for example, the first column provides the coordinates of the first point and the column length indicates whether it is 2D vs. 3D. First, compute the area of the triangle (the first line of your output file). Second, using the first two points, construct a line if 2D or a plane if 3D (the bisector of the two points) and find the distance of the third point to that line or plane (the second line of your output file). The output should have two numbers in separate lines. The output should contain numbers with up to four significant digits.

The input for Part C can be a 2x3 matrix or alternatively a 3x3 matrix. Your program should be able to process both input file formats.

Input format (3x3 matrix):

n11 n12 n13

n21 n22 n23

n31 n32 n33

In python please

Solutions

Expert Solution

Reading the 3 points as in the matrix to lists from input text file, mydata.txt

first_point = []
second_point = []
third_point = []
with open("/content/mydata.txt") as file:
    lines = [line.strip() for line in file]
for i in range(len(lines)):
    row = lines[i].split(" ")
    first_point.append(int(row[0]))
    second_point.append(int(row[1]))
    third_point.append(int(row[2]))
print(first_point,second_point,third_point)

Function to compute the distance between two points

def distance(p1,p2):  
    if len(p1) == 3:
      d = math.sqrt(math.pow(p1[0] - p2[0], 2) +
                math.pow(p1[1] - p2[1], 2) +
                math.pow(p2[2] - p2[2], 2)* 1.0)
    else:
      d =  math.sqrt(math.pow(p1[0] - p2[0], 2) +
                math.pow(p1[1] - p2[1], 2)* 1.0)
    print(d)
    return d

Computing the area of the triangle formed by the 3 points

d1 = distance(first_point,second_point)
d2 = distance(second_point,third_point)
d3 = distance(third_point,second_point)

p = (d1 + d2 + d3)/2
area = math.sqrt(p * (p - d1) * (p - d2) * (p - d3))

using the first two points, construct a line if 2D or a plane if 3D (the bisector of the two points) and find the distance of the third point to that line or plane (the second line of your output file)

import numpy as np

p1 = np.array(first_point)
p2 = np.array(second_point)
p3 = np.array(third_point)

if len(lines) == 2:
  a = p2[1] - p1[1]
  b = p1[0] - p2[0]
  c = a*(p1[0]) + b*(p1[1])
  dist = abs((a * p3[0] + b * p3[1] + c)) / (math.sqrt(a * a + b * b))

if len(lines) == 3:
  # These two vectors are in the plane
  v1 = p3 - p1
  v2 = p2 - p1

  # the cross product is a vector normal to the plane 
  cp = np.cross(v1, v2)
  a,b,c = cp

  # This evaluates a * x3 + b * y3 + c * z3 which equals d
  d = np.dot(cp, p3)

  d = abs((a * p3[0] + b * p3[1] + c * p3[2] + d))  
  e = (math.sqrt(a * a + b * b + c * c)) 
  dist = d/e

writing the area and the distance into output file

f = open('output.txt', 'w+')
f.write("%4f\n" % area)
f.write("%4f\n" % dist)

Related Solutions

Given the matrix A (2x2 matrix taking the first two column vectors from the input file),...
Given the matrix A (2x2 matrix taking the first two column vectors from the input file), compute the followings. Λ: the diagonal matrix whose diagonal elements are the corresponding eignevalues Λii = λi for i=1,2 R: the 2x2 matrix whose ith column vector is the eigenvector corresponding to λi for i=1,2 RΛRT: the matrix compositions 1/0: the comparison between A and RΛRT (is A= RΛRT?) Your output should have seven lines where the first two lines correspond to the 2x2...
Requirements: a.   Copy the trial balance above in the first two columns of a 10-column work...
Requirements: a.   Copy the trial balance above in the first two columns of a 10-column work sheet. b.   Prepare adjusting journal entries in journal form from the following information (the fiscal year ends December 31). 1.   Estimated bad debts, .5% of sales less returns and allowances. 2.   Depreciation on building 4% per year; on furniture and fixtures, 10% per year. 3.   Insurance expired during the year, $3,900. 4.   Interest at 12% is payable on the mortgage on January 1 of...
Find the polar coordinates of a point with the Cartesian coordinates
 Find the polar coordinates of a point with the Cartesian coordinates (x,y)=(−4√2,4√2).
Plot the point whose polar coordinates are given. Then find the Cartesian coordinates of the point...
Plot the point whose polar coordinates are given. Then find the Cartesian coordinates of the point b. (2, π/4) c.(−3, −π/6)
A six-column table for JKL Company follows. The first two columns contain the unadjusted trial balance...
A six-column table for JKL Company follows. The first two columns contain the unadjusted trial balance for the company as of July 31, 2017. The last two columns contain the adjusted trial balance as of the same date. Unadjusted Trial Balance Adjusted Trial Balance Cash $ 108,120 $ 108,120 Accounts receivable 10,000 20,000 Office supplies 17,200 6,500 Prepaid insurance 7,540 3,160 Office equipment 90,000 90,000 Accum. Depreciation—Office equip. $ 25,000 $ 32,000 Accounts payable 10,100 24,000 Interest payable 0 3,000...
A six-column table for JKL Company follows. The first two columns contain the unadjusted trial balance...
A six-column table for JKL Company follows. The first two columns contain the unadjusted trial balance for the company as of July 31, 2017. The last two columns contain the adjusted trial balance as of the same date. Unadjusted Trial Balance Adjusted Trial Balance Cash $ 34,000 $ 34,000 Accounts receivable 14,000 22,000 Office supplies 16,000 2,000 Prepaid insurance 8,540 2,960 Office equipment 84,000 84,000 Accum. Depreciation—Office equip. $ 14,000 $ 20,000 Accounts payable 9,100 10,000 Interest payable 0 1,000...
A six-column table for JKL Company follows. The first two columns contain the unadjusted trial balance...
A six-column table for JKL Company follows. The first two columns contain the unadjusted trial balance for the company as of July 31, 2017. The last two columns contain the adjusted trial balance as of the same date. Unadjusted Trial Balance Adjusted Trial Balance Cash $ 34,000 $ 34,000 Accounts receivable 14,000 22,000 Office supplies 16,000 2,000 Prepaid insurance 8,540 2,960 Office equipment 84,000 84,000 Accum. Depreciation—Office equip. $ 14,000 $ 20,000 Accounts payable 9,100 10,000 Interest payable 0 1,000...
Create a class named “Car” which has the following fields. The fields correspond to the columns...
Create a class named “Car” which has the following fields. The fields correspond to the columns in the text file except the last one. i. Vehicle_Name : String ii. Engine_Number : String iii. Vehicle_Price : double iv. Profit : double v. Total_Price : double (Total_Price = Vehicle_Price + Vehicle_Price* Profit/100) 2. Write a Java program to read the content of the text file. Each row has the attributes of one Car Object (except Total_Price). 3. After reading the instances of...
Java. Given an input file with each line representing a record of data and the first...
Java. Given an input file with each line representing a record of data and the first token (word) being the key that the file is sorted on, we want to load it and output the line number and record for any duplicate keys we encounter. Remember we are assuming the file is sorted by the key and we want to output to the screen the records (and line numbers) with duplicate keys. We are given a text file and have...
Java Write a program that will only accept input from a file provided as the first...
Java Write a program that will only accept input from a file provided as the first command line argument. If no file is given or the file cannot be opened, simply print “Error opening file!” and stop executing. A valid input file should contain two lines. The first line is any valid string, and the second line should contain a single integer. The program should then print the single character from the string provided as the first line of input...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT