javascript - Get rotated rectangle points from x, y, width, height and rotation - Stack Overflow

I have a rectangle which is rotated (in this case 45 degrees) and looks like this: And I know that X is

I have a rectangle which is rotated (in this case 45 degrees) and looks like this:

And I know that X is from the top left corner of the rectangle (if it were unrotated, in this case the point at the top of the picture). Y is also from the top left corner. I have the width and the height and the bounding box. I want to find out what the other points of this rectangle are. The top left (technically the X position in this case), the top right, the bottom right and the bottom left. I was trying to use a transformation matrix but I can't seem to wrap my head around it.

How would one find the other points of this rectangle? Technically I am working in JavaScript but any language should be able to deal with this problem.

I have a rectangle which is rotated (in this case 45 degrees) and looks like this:

And I know that X is from the top left corner of the rectangle (if it were unrotated, in this case the point at the top of the picture). Y is also from the top left corner. I have the width and the height and the bounding box. I want to find out what the other points of this rectangle are. The top left (technically the X position in this case), the top right, the bottom right and the bottom left. I was trying to use a transformation matrix but I can't seem to wrap my head around it.

How would one find the other points of this rectangle? Technically I am working in JavaScript but any language should be able to deal with this problem.

Share Improve this question asked Jul 10, 2016 at 22:42 JohnstonJohnston 20.9k22 gold badges76 silver badges124 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

for anybody else looking for this. Here is a function to do so.

x,y,height and width are as shown in the picture below. ang is the angle between the x,y point and the Y-Axis. if you want the one between the x,y point and the X-Axis simply do so: ang = 90 - ang before sending it to the function

isDeg is simply whether you are sending the ang in Radians or Degrees.

function getRectFourPoints(x,y, width, height, ang, isDeg = false) {
  	
	if(isDeg) ang = ang * (Math.PI / 180)

	const points = {first: {x,y}}
	const sinAng = Math.sin(ang)	
	const cosAng = Math.cos(ang)
	
	let upDiff = sinAng * width
	let sideDiff = cosAng * width
	const sec = {x: x + sideDiff, y: y + upDiff}
	points.sec = sec
	
	upDiff = cosAng * height
	sideDiff = sinAng * height
	points.third = {x: x + sideDiff, y: y - upDiff}
	
	const fourth = {x: sec.x + sideDiff, y: sec.y - upDiff}
	points.fourth = fourth
	return points
}

The rotation matrix can help to calculate its current position based on its previous one. If it's rotating clockwise, the 2D rotation matrix is as follows:

which makes

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744732250a4590535.html

相关推荐

  • 如何让AI更懂你?大模型个性化微调技术解析

    如何让AI更懂你?大模型个性化微调技术解析引言在人工智能领域,大型预训练模型(如GPT、BERT等)已经成为许多任务的核心工具。然而,这些模型虽然强大,但在处理特定领域的任务时,往往无法完全满足用户的需求。为了让AI更懂你,个性化微调技术成

    2小时前
    00
  • AI技术学习日志:从零到一的突破与高效掌握技巧

    一、学习初期:迷茫与探索(一)入门准备最初接触AI时,面对众多的概念(如机器学习、深度学习、神经网络等)感到无比迷茫。为了建立起基本的知识框架,我开始阅读一些入门书籍,像《人工智能:一种现代方法》和《Python机器学习基础教程》。这些书籍

    2小时前
    00
  • 了解HarmonyOS NEXT工程目录结构

    创建第一个工程如果你是首次打开DevEco Studio,那么首先会进入欢迎页。在欢迎页中单击“Create Project”,进入项目创建页面。选择‘Application’,然后选择‘Empty Ability’,单击‘Next’进入项

    2小时前
    00
  • 我“AI”发文——AI在法律、医疗、金融等领域的应用前景

    我“AI”发文——AI在法律、医疗、金融等领域的应用前景人工智能(AI)正在深刻影响各个行业,其中法律、医疗和金融领域尤为突出。AI技术的快速发展,使其在提高效率、降低成本、优化决策、增强服务质量等方面发挥了重要作用。本文将详细探讨AI在这

    2小时前
    10
  • MinerU本地化部署教程——一款AI知识库建站的必备工具

    前言:来一个官方一点的介绍:MinerU是一个一站式、开源、高质量的数据提取工具,支持多种功能,如提取 PDF、markdown 等格式的内容。MinerU可以用来做什么?现在很多公司和个人都喜欢借助例如 MaxKB、Dify、Anythi

    2小时前
    10
  • SEM

    在集成电路制造领域,纳米级晶圆缺陷的检测与分类对于后续的根本原因分析及产量提升至关重要。扫描电子显微镜(SEM)图像中存在的复杂背景图案以及缺陷多样化的纹理特征构成了重大挑战。传统方法通常受限于数据不足、标签欠缺和较差的迁移性。在本文中

    2小时前
    00
  • Bacula开源备份系统,企业级数据保护的利器!

    大家好,波哥又来给大家推荐好东西啦!欢迎大家在评论区留言评论自己想了解的工具、方向或职业等互联网相关内容,点赞和推荐多的,波哥会优先安排解答!简介Bacula 是一个开源的企业级计算机备份系统,设计用于处理不同系统的网络,自动完成备份任务

    1小时前
    10
  • Nginx再见!Caddy你好!Caddy 无需配置自动添加 HTTPS,我真的爱了!

    前言在当今的网站部署中,HTTPS 已成为标配而非可选功能。传统方式配置 HTTPS 需要手动申请证书、配置 Nginx 等复杂步骤,Caddy 的简洁配置和自动化 HTTPS 使其成为静态网站部署的理想选择。下面是我在我的服务器上使用 C

    1小时前
    00
  • Java + LangChain = 王炸!

    在 Baeldung 上看到了一篇介绍基于 Java + LangChain 开发大语言模型应用的基础入门文章,写的非常不错,非常适合初学者。我的公众号应该有很多读者对这方面的知识感兴趣,希望这篇文章能够起到入门的作用。原文地址:翻译:J

    1小时前
    00
  • 傻瓜式复制AI提示词模板,手把手教你用AI搞定复杂Git操作

    前端开发中,你是不是也遇到过下面的场景:Git分支一旦冲突就手忙脚乱?想要清理提交历史却无从下手?对复杂的Git操作充满不确定感?今天,我们提供一个傻瓜式AI提示模板,你只需要简单复制并填写自己的Git问题,AI就会逐步、详细地为你给出精确

    1小时前
    00
  • 教你 1 小时搭建属于自己的 AI 知识库,Ollama + MaxKB 超详细教程略

    教你 1 小时搭建属于自己的 AI 知识库,Ollama + MaxKB 超详细教程略前言网络组建不易,学习网络知识就要循序渐进,别急,看看这个文章,掌握如何通过 、、、络!作者:神的孩子都在歌唱我们经常为海量文档找不到关键内容而烦恼,那

    1小时前
    00
  • 当结构变异遇到三代测序会有什么故事

    研究背景基因组结构变异(SVs)包括缺失、重复、倒位、插入和易位等,对人类疾病和个体基因组差异具有重要影响。长读测序技术(如PacBio和Oxford Nanopore)的发展为SVs的检测提供了更有力的工具,但目前缺乏对基于比对和基于组装

    1小时前
    00
  • Java + LangChain 开发大语言模型应用!

    在 Baeldung 上看到了一篇介绍基于 Java + LangChain 开发大语言模型应用的基础入门文章,写的非常不错,非常适合初学者。于是,我抽空翻译了一下。1. 简介在本教程中,我们将详细探讨 LangChain[1],一个用于开

    1小时前
    00
  • 3D领域DeepSeek「源神」启动!国产明星创业公司,一口气开源八大项目

    机器之心报道机器之心编辑部2025 开年,DeepSeek-R1 的成功在全球掀起了一股开源风潮,上个月的开源周更是毫无保留地将自己的多项核心技术开放给了全球开发者。这种「完整技术栈」式的开源震撼了整个行业。毫无疑问,开源正成为国内外大模型

    1小时前
    00
  • 开发体育赛事直播系统:炫彩弹幕直播间界面技术实现方案

    体育赛事直播系统的炫彩弹幕直播间界面,“东莞梦幻网络科技”的源码技术实现方案,包括前端(Vue.js)、后端(ThinkPHP)、安卓(Java)和iOS(Objective-C)的实现代码。系统架构设计技术栈后端: PHP (ThinkP

    57分钟前
    00
  • 正点原子DM40

    抱歉,最早还吐槽,狗都不买,嘿嘿当狗了:淘宝的图,我的还在路上鄙人是不和10V以上的电子系统打交道的,所以就是最低的版本了我就用用它的万用表功能,示波器太差了,信号发生器也差。想知道这东西的方案。器件倒是挺好看的这是拆了盖子的样子GD32的

    51分钟前
    10
  • 中台已死,十年再话

    中台作为中国互联网技术领域最玄幻的名词之一,曾经充斥在各种技术大会和公众号,面试高阶产品研发人才不探讨下中台都觉得差点儿意思。围绕中台诞生了无数热帖、无数架构图,各路大神现身说法布道授业解惑。你也说中台,我也说中台,仿佛万物皆可中台。各大出

    49分钟前
    00
  • utterances

    多听,少说,接受每一个人的责难,但是保留你的最后裁决。——莎士比亚《哈姆雷特》GitHub - utteranceutterances:

    36分钟前
    00
  • 【redis】集群 主节点宕机后选择 master 的详细流程

    使用集群此时,使用客户端连上集群中的任何一个节点,都相当于连上了整个集群从 101-109 这个九个节点,是一个整体可以通过 cluster nodes 查看整个集群的情况image.png前面的 6379 是业务端口,后面的 16379

    11分钟前
    00
  • 【蓝桥杯每日一题】3.28

    喵~ 今天要学习的算法是双指针,也被称为滑动窗口是⼀种优化暴⼒枚举策略的⼿段: • 当我们发现在两层 for 循环的暴⼒枚举过程中,两个指针是可以不回退的,此时我们就可以利⽤两个指针不回退的性质来优化时间复杂度。一、唯一的雪

    6分钟前
    00

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信