第六章第三十九題(幾何:點的位置)(Geometry: point position) - 編程練習題答案

来源:https://www.cnblogs.com/in2013/archive/2020/06/12/13097190.html
-Advertisement-
Play Games

6.39(幾何:點的位置)編程練習題3.32顯示如何測試一個點是否在一個有向直線的左側、右側,或在該直線上。使用下麵的方法頭編寫該方法: public static boolean leftOfTheLine(double x0, double y0, double x1, double y1, d ...


6.39(幾何:點的位置)編程練習題3.32顯示如何測試一個點是否在一個有向直線的左側、右側,或在該直線上。使用下麵的方法頭編寫該方法:

public static boolean leftOfTheLine(double x0, double y0, double x1, double y1, double x2, double y2)

public static boolean onTheSameLine(double x0, double y0, double x1, double y1, double x2, double y2)

public static boolean onTheLineSegment(double x0, double y0, double x1, double y1, double x2, double y2)

編寫一個程式,提示用戶輸入三個點賦給p0、p1和p2,顯示p2是否在從p0到p1的直線的左側、右側、直線上,或者線段上。

下麵是一些運行示例:

Enter three points for p0, p1, and p2: 1 1 2 2 1.5 1.5

(1.5, 1.5) is on the line segment from (1.0, 1.0) to (2.0, 2.0)

Enter three points for p0, p1, and p2: 1 1 2 2 3 3

(3.0, 3.0) is on the same line from (1.0, 1.0) to (2.0, 2.0)

Enter three points for p0, p1, and p2: 1 1 2 2 1 1.5

(1.0, 1.5) is on the left side of the line from (1.0, 1.0) to (2.0, 2.0)

Enter three points for p0, p1, and p2: 1 1 2 2 1 –1

(1.0, −1.0) is on the right side of the line from (1.0, 1.0) to (2.0, 2.0)

6.39(Geometry: point position) Programming Exercise 3.32 shows how to test whether a point is on the left side of a directed line, on the right, or on the same line. Write the methods with the following headers:

public static boolean leftOfTheLine(double x0, double y0, double x1, double y1, double x2, double y2)

public static boolean onTheSameLine(double x0, double y0, double x1, double y1, double x2, double y2)

public static boolean onTheLineSegment(double x0, double y0, double x1, double y1, double x2, double y2)

Write a program that prompts the user to enter the three points for p0, p1, and p2and displays whether p2 is on the left side of the line from p0 to p1, right side, the same line, or on the line segment.

Here are some sample runs:

Enter three points for p0, p1, and p2: 1 1 2 2 1.5 1.5

(1.5, 1.5) is on the line segment from (1.0, 1.0) to (2.0, 2.0)

Enter three points for p0, p1, and p2: 1 1 2 2 3 3

(3.0, 3.0) is on the same line from (1.0, 1.0) to (2.0, 2.0)

Enter three points for p0, p1, and p2: 1 1 2 2 1 1.5

(1.0, 1.5) is on the left side of the line from (1.0, 1.0) to (2.0, 2.0)

Enter three points for p0, p1, and p2: 1 1 2 2 1 –1

(1.0, −1.0) is on the right side of the line from (1.0, 1.0) to (2.0, 2.0)

下麵是參考答案代碼:

// https://cn.fankuiba.com
import java.util.Scanner;

public class Ans6_39_page205 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter three points for p0, p1, and p2:");
        double x0 = input.nextDouble();
        double y0 = input.nextDouble();
        double x1 = input.nextDouble();
        double y1 = input.nextDouble();
        double x2 = input.nextDouble();
        double y2 = input.nextDouble();

        if (onTheSameLine(x0, y0, x1, y1, x2, y2)) {
            if (onTheLineSegment(x0, y0, x1, y1, x2, y2)) {
                System.out.printf("(%.1f, %.1f) is on the line segment from "
                        + "(%.1f, %.1f) to (%.1f, %.1f)", x2, y2, x0, y0, x1, y1);
            } else {
                System.out.printf("(%.1f, %.1f) is on the same line from "
                        + "(%.1f, %.1f) to (%.1f, %.1f)", x2, y2, x0, y0, x1, y1);
            }
        } else {
            if (leftOfTheLine(x0, y0, x1, y1, x2, y2)) {
                System.out.printf("(%.1f, %.1f) is on the left side of the line from "
                        + "(%.1f, %.1f) to (%.1f, %.1f)", x2, y2, x0, y0, x1, y1);
            } else {
                System.out.printf("(%.1f, %.1f) is on the right side of the line from "
                        + "(%.1f, %.1f) to (%.1f, %.1f)", x2, y2, x0, y0, x1, y1);
            }
        }
    }

    public static boolean leftOfTheLine(double x0, double y0,
                                        double x1, double y1, double x2, double y2) {
        if ((x1 - x0) * (y2 - y0) - (x2 - x0) * (y1 - y0) > 0)
            return true;
        else
            return false;
    }

    public static boolean onTheSameLine(double x0, double y0,
                                        double x1, double y1, double x2, double y2) {
        return (x1 - x0) * (y2 - y0) - (x2 - x0) * (y1 - y0) == 0;
    }

    public static boolean onTheLineSegment(double x0, double y0,
                                           double x1, double y1, double x2, double y2) {
        if ((x1 - x0) * (y2 - y0) - (x2 - x0) * (y1 - y0) == 0 &&
                x2 <= (Math.max(x0, x1)) && x2 >= (Math.min(x1, x0)))
            return true;
        else
            return false;
    }
}

適用Java語言程式設計與數據結構(基礎篇)(原書第11版)Java語言程式設計(基礎篇)(原書第10/11版)

發佈在博客:(https://cn.fankuiba.com)


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • JavaScript是一種屬於網路的腳本語言,已經被廣泛用於Web應用開發,常用來為網頁添加各式各樣的動態功能,為用戶提供更流暢美觀的瀏覽效果。通常JavaScript腳本是通過嵌入在HTML中來實現自身的功能的。 CSS(Cascading Style Sheet)層疊樣式表單 表現(presen ...
  • ie8下透明度處理 css3新增屬性rgba和opacity:(0~1); 在ie8下無法使用 Alpha(opacity=[0~100]);//[0~100] 0是透明 100是不透明。 IE下的透明度屬性,子元素會繼承這個透明度。下麵有阻斷子元素繼承方法。 解決方法: 背景透明,文字不透明。 < ...
  • Vue計算屬性 1、提到計算屬性,我們馬上就會想到它的一個特性:緩存,Vue 文檔也如是說: 計算屬性是基於它們的響應式依賴進行緩存的/ 2。很榮幸給大家分享,我是一名08年出道的前端高級老鳥,大家如果想跟我交流學習經驗,可以進我的扣扣裙 519293536 有問題我都會儘力幫大家,喜歡中高級問題, ...
  • 在Vue項目中,引入到工程中的所有js、css文件,編譯時都會被打包進vendor.js,瀏覽器在載入該文件之後才能開始顯示首屏。若是引入的庫眾多,那麼vendor.js文件體積將會相當的大,影響首屏的體驗。可以看個例子: 差點忘記介紹了:我是一名08年出道的高級前端老鳥,大家如果想跟我交流學習經驗 ...
  • 別天天販賣焦慮,能當上程式員就已經幹掉多少億人了? 程式員不優秀嗎?很優秀啦!你非要跟那些運氣好的,出身好的,趕上紅利期的,比你玩命的比,那不能焦慮嗎? 程式員肯定是一個小圈子。程式員距離過剩還遠,只是門檻越來越高,你感嘆飽和只是你想不努力就掙錢,哪有那麼簡單的事情。 有大量的人學習程式,但是真正能 ...
  • 先介紹下大致情況時間線。 18 年 8 月正式轉方向為前端,之前做了一段時間的 iOS,後來因為對前端更感興趣所以就打算轉方向了。19 年 10 月入職當前公司,定級資深前端,分配到業務架構小組,自此在一年零兩月的時間內完成從 iOS 轉方向到資深前端的過程。 很多讀者會問我是如何學習的,今天這篇文 ...
  • 效果圖 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>index ...
  • 作者:凹凸曼-吖偉 我們在平時編程開發時,除了需要關註技術實現、演算法、代碼效率等因素之外,更要把所學到的學科知識(如物理學、理論數學等等)靈活應用,畢竟理論和實踐相輔相成、密不可分,這無論是對於我們的方案選型、還是技術實踐理解都有非常大的幫助。今天就讓我們一起來回顧中學物理知識,並靈活運用到慣性滾動 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...