JavaScript 程序查找直线的中点 (Program to find the mid-point of a line)

 给定一条线的两个坐标,起点为 (x1,y1),终点为 (x2,y2),找出该线的中点,如下面示例图所示:

图1 

图2 

图3 

        查找一条直线的中点可以通过简单的数学公式实现。中点的坐标是两个端点坐标的平均值。假设直线的两个端点分别为 (A(x_1, y_1)) 和 (B(x_2, y_2)),中点 (M(x_m, y_m)) 的坐标,如下:

示例: 

输入  : x1 = –1, y1 = 2, 
         x2 = 3, y2 = –6
输出 : 1,–2

输入  : x1 = 6.4, y1 = 3 
         x2 = –10.7, y2 = 4
输出 : –2.15, 3.5 

        中点公式:两点 (x1, y2) 和 (x2, y2) 的中点是通过以下公式找到的点 M:M = ((x1+x2)/2 , (y1+y2)/2)

示例代码:  

// JavaScript program to find  
// the midpoint of a line 
  
// function to find the 
// midpoint of a line 


    function midpoint(x1, x2, 
                          y1, y2) 
    { 
        document.write((x1 + x2) / 2 + 
                        " , " + (y1 + y2) / 2) ;  
    } 
  
// Driver code 
          
let x1 =-1, y1 = 2 ; 


let x2 = 3, y2 = -6 ;  

midpoint(x1, x2, y1, y2); 
                     

输出 : 

1 , -2

时间复杂度: O(1),因为只执行常量操作

辅助空间: O(1)

  • 8
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Hi! I apologize for any confusion, but as a text-based AI model, I don't have the capability to enable or interact with specific browser settings like JavaScript. However, I can provide you with some general guidance on how to enable JavaScript in commonly used browsers. To enable JavaScript in your browser, please follow these steps: 1. Google Chrome: - Click on the three-dot menu in the top-right corner and select "Settings". - Scroll down and click on "Advanced" to expand the advanced settings. - Under the "Privacy and security" section, click on "Site Settings". - Find and click on "JavaScript" to access the JavaScript settings. - Toggle the switch to enable JavaScript. 2. Mozilla Firefox: - Click on the three-line menu in the top-right corner and select "Options". - In the left sidebar, click on "Privacy & Security". - Under the "Permissions" section, check the box next to "Enable JavaScript". - Close the options tab to save the changes. 3. Microsoft Edge: - Click on the three-dot menu in the top-right corner and select "Settings". - Scroll down and click on "Cookies and site permissions". - Click on "JavaScript" to access the JavaScript settings. - Toggle the switch to enable JavaScript. Please note that these instructions may vary slightly depending on the browser version you are using. Once you have enabled JavaScript in your browser, you should be able to utilize Ant Design without any issues. If you have any further questions or if there's something else I can help you with, please let me know!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值