简体   繁体   English

如何在基于x,y和z的3D矩形中绘制线?

[英]How to Draw line in 3D rectangle based on x,y and z?

在此处输入图片说明

I would like draw 3D points represented in image to 3D rectangle. 我想将图像中表示的3D点绘制为3D矩形。 Any idea how could I represent these in x,y and z axis 知道如何在x,y和z轴上表示这些

Here projection type is orthographic. 在这里投影类型是正交的。

Thanks 谢谢

Okay. 好的。 Let's look at a simple example of what you are trying to accomplish it, and why this is such a complicated problem. 让我们看一个简单的示例,说明您要完成什么,以及为什么这是一个如此复杂的问题。

First, lets look a some projection functions. 首先,让我们看一些投影功能。 You need a way to mathematically describe how to transform a 3D (or higher dimensional) point into a 2D space (your monitor), or a projection . 您需要一种数学上描述如何将3D(或更高维)点转换为2D空间(显示器)或投影的方法

The simpiest to understand is a very simple dimetric projection. 最容易理解的是非常简单的二元投影。 Something like: 就像是:

x' = x + z/2;
y' = y + z/4;

What does this mean? 这是什么意思? Well, x' is you x coordinate 2D projection : for every unit you move backwards in space, the projection will move that point half that many units to the right . 好吧, x'是x坐标2D 投影 :对于在空间中向后移动的每个单位, 投影都会将该点向右移动一半单位。 And y' represents that same projection for your y coordinate: for every unit you move backwards in space, the projection will move that point a quarter unit up . y'表示y坐标的相同投影:对于空间中向后移动的每个单位,该投影将使该点向上移动四分之一单位。

So a point at [0,0,0] will get projected to a 2d point of [0,0] . 因此,在[0,0,0]点将投影到[0,0]的2d点。 A point at [0,0,4] will get projected to a 2d point of [2,1] . [0,0,4]点将投影到[2,1]的2d点。

Implemented in JavaScript, it would look something like this: 用JavaScript实现,看起来像这样:

// Dimetric projection functions
var dimetricTx = function(x,y,z) { return x + z/2; };
var dimetricTy = function(x,y,z) { return y + z/4; };

Once you have these projection functions -- or ways to translate from 3D space into 2D space -- you can use them to start draw your image. 一旦有了这些投影功能-或从3D空间转换为2D空间的方法-您就可以使用它们开始绘制图像。 A simple example of that using js canvas. 一个使用js canvas的简单示例。 First, some context stuff: 首先,一些上下文的东西:

var c = document.getElementById("cnvs");
var ctx = c.getContext("2d");

Now, lets make a little helper to draw a 3D point: 现在,让一个小帮手绘制一个3D点:

  var drawPoint = (function(ctx,tx,ty, size) {
    return function(p) {
      size = size || 3;

      // Draw "point"
      ctx.save();
      ctx.fillStyle="#f00";
      ctx.translate(tx.apply(undefined, p), ty.apply(undefined,p));
      ctx.beginPath();
      ctx.arc(0,0,size,0,Math.PI*2);
      ctx.fill();
      ctx.restore();
    };
  })(ctx,dimetricTx,dimetricTy);

This is pretty simple function, we are injecting the canvas context as ctx , as well as our tx and ty functions, which in this case our the dimetric functions we saw earlier. 这是一个非常简单的函数,我们将canvas上下文作为ctx注入,以及我们的txty函数,在这种情况下,这是我们之前看到的dimetric函数。

And now a polygon drawer: 现在是一个多边形抽屉:

  var drawPoly = (function(ctx,tx,ty) {
    return function() {
      var args = Array.prototype.slice.call(arguments, 0);

      // Begin the path
      ctx.beginPath();

      // Move to the first point
      var p = args.pop();
      if(p) {
        ctx.moveTo(tx.apply(undefined, p), ty.apply(undefined, p));
      }

      // Draw to the next point
      while((p = args.pop()) !== undefined) {
        ctx.lineTo(tx.apply(undefined, p), ty.apply(undefined, p));
      }

      ctx.closePath();
      ctx.stroke();

    };
  })(ctx, dimetricTx, dimetricTy);

With those two functions, you could effectively draw the kind of graph you are looking for. 使用这两个功能,您可以有效地绘制所需的图形。 For example: 例如:

// The array of points
var points = [
  // [x,y,z]
  [20,30,40],
  [100,70,110],
  [30,30,75]
];

(function(width, height, depth, points) {
  var c = document.getElementById("cnvs");
  var ctx = c.getContext("2d");

  // Set some context
  ctx.save();
  ctx.scale(1,-1);
  ctx.translate(0,-c.height);

  ctx.save();

  // Move our graph
  ctx.translate(100,20);  

  // Draw the "container"
  ctx.strokeStyle="#999";
  drawPoly([0,0,depth],[0,height,depth],[width,height,depth],[width,0,depth]);

  drawPoly([0,0,0],[0,0,depth],[0,height,depth],[0,height,0]);
  drawPoly([width,0,0],[width,0,depth],[width,height,depth],[width,height,0]);
  drawPoly([0,0,0],[0,height,0],[width,height,0],[width,0,0]);
  ctx.stroke();

  // Draw the points
  for(var i=0;i<points.length;i++) {
    drawPoint(points[i]);
  }

})(150,100,150,points);

However, you should now be able to start to see some of the complexity of your actual question emerge. 但是,您现在应该可以开始看到实际问题的某些复杂性了。 Namely, you asked about rotation, in this example we are using an extremely simple projection (our dimetric projection) which doesn't take much other than an oversimplified relationship between depth and its influences on x,y position. 也就是说,您询问了旋转问题,在此示例中,我们使用的是非常简单的投影(我们的二元投影),除了深度与其对x,y位置的影响之间的关系过分简化之外,不需要太多操作。 As the projections become more complex, you need to know more about your relationship/orientation in 3D space in order to create a reasonable 2D projection. 随着投影变得越来越复杂,您需要更多地了解3D空间中的关系/方向,以便创建合理的2D投影。

A working example of the above code can be found here . 上面的代码的工作示例可以在这里找到 The example also includes isometric projection functions that can be swapped out for the dimetric ones to see how that changes the way the graph looks. 该示例还包括等距投影函数,可以将其替换为二等距投影函数,以查看其如何改变图形的外观。 It also does some different visualization stuff that I didn't include here, like drawing "shadows" to help "visualize" the actual orientation -- the limitations of 3D to 2D projections. 它还执行一些我未在此处包含的不同可视化内容,例如绘制“阴影”以帮助“可视化”实际方向-3D到2D投影的局限性。

It's complicated, and even a superficial discussion is kind of beyond the scope of this stackoverflow. 这很复杂,甚至是一个肤浅的讨论都超出了这个stackoverflow的范围。 I recommend you read more into the mathematics behind 3D, there are plenty of resources, both online and in print form. 我建议您阅读3D背后的数学知识,在线和印刷形式的资源很多。 Once you have a more solid understanding of the basics of how the math works then return here if you have a specific implementation question about it. 对数学工作原理有了更深入的了解后,如果对数学有具体的实现问题,请返回此处。

What you want to do is impossible to do using the method you've stated - this is because a box - when rotated in 3 dimensions won't look anything like that diagram of yours. 您想要做的事情是无法使用您陈述的方法完成的-这是因为一个盒子-在3维旋转时看起来不会像您的图表那样。 It will also vary based on the type of projection you need. 它也会根据您需要的投影类型而有所不同。 You can, however get started using three.js which is a 3D drawing library for Javascript. 但是,您可以开始使用three.js ,这是Javascript的3D图形库。

Hope this helps. 希望这可以帮助。

How to Draw 3D Rectangle? 如何绘制3D矩形? posted in: Parallelogram | 发表于:平行四边形| updated on: 14 Sep, 2012 更新日期:2012年9月14日

To sketch 3 - Dimensional Rectangle means we are dealing with the figures which are different from 2 – D figures, which would need 3 axes to represent them. 绘制3维矩形表示我们要处理的图形与2维图形不同,后者需要3个轴来表示它们。 So, how to draw 3D rectangle? 那么,如何绘制3D矩形呢?

To start with, first make two lines, one vertical and another horizontal in the middle of the paper such that they represent a “t” letter of English. 首先,首先在纸的中间画两条线,一条垂直线,另一条水平线,使其代表英文的“ t”字母。 This is what we need to draw for temporary use and will be removed later after the construction of the 3 – D rectangle is complete. 这是我们暂时需要绘制的内容,将在3-D矩形的构造完成后将其删除。 Next we draw a Square whose measure of each side is 1 inch. 接下来,我们绘制一个正方形,每个边的尺寸为1英寸。 Square must be perfect in Geometry so that 90 degree angles that are formed at respective corners are exact in measure. 正方形在几何形状上必须是完美的,以便在各个角上形成的90度角精确测量。 Now starting from upper right corner of the square we draw a line segment that will be stretched to a measure of 2 inches in the direction at an angle of 45 degrees. 现在从正方形的右上角开始绘制一条线段,该线段将以45度角的方向拉伸到2英寸。 Similarly, we repeat the procedure by drawing another Line Segment from the upper left corner of the square and stretching it to 2 inches length in the direction at an angle of 45 degrees. 类似地,我们通过从正方形的左上角绘制另一个“线段”并以45度角的方向将其拉伸到2英寸长来重复此过程。 These 2 line segments are considered to be the diagonals with respect to the horizontal line that we drew temporarily in starting. 这两个线段被认为是相对于我们在开始时临时绘制的水平线的对角线。 Also these lines will be parallel to each other. 这些线也将彼此平行。 Next we draw a line that joins the end Point of these two diagonals. 接下来,我们绘制一条连接这两个对角线端点的线。

Next starting from the very right of the 2 inch diagonal end point, draw a line of measure 1 inch that is supposed to be perpendicular to the temporary horizontal line. 接下来,从2英寸对角线端点的最右边开始,绘制一条应该垂直于临时水平线的1英寸测量线。 Next we need to join the lower left corner of the square with end point of the last 1'' line we drew in 4th step and finally we get our 3 - D rectangular. 接下来,我们需要将正方形的左下角与我们在第四步中绘制的最后1''线的端点连接起来,最后得到3-D矩形。 Now we can erase our initial “t”. 现在我们可以删除初始的“ t”。 This 3- D rectangle resembles a Cuboid. 此3-D矩形类似于长方体。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将使用x / y / z设置的轮廓数据转换为3d对象 - how to convert contour data set with x/y/z to 3d object JavaScript Plotly.js 如何使用 x、y 和 z 坐标创建 3D 表面 plot? - JavaScript Plotly.js How to create a 3D surface plot using x, y, and z coordinates? 如何创建一个d3线函数,该函数将为x,y1,y2数据绘制2条线? - How do you create a d3 line function that will draw 2 lines for x, y1, y2 data? Three.js 2D X,Y坐标始终位于镜头前的3D X,Y,Z坐标 - Three.js 2D X, Y coords to 3D X, Y, Z coords always in front of camera 如何在Matrix3d上获得比例尺(x,y,z)和旋转量(x,y,z)? - How to get the scale(x,y,z) together with rotation(x,y,z) on a matrix3d? d3-在基于y坐标的位置绘制网格线? - d3-Draw grid line at a location based on the y coordinates? 根据半径,x和y 3D地球仪THREE.js确定3D经度和纬度 - Determine 3D Longitude and Latitude based on radius, x and y 3D globe THREE.js Three.js-禁用沿x,y和z轴的3d对象平移 - Three.js - Disable 3d object translations along x, y and z axis 如何在JavaScript中计算旋转3D立方体的x和y坐标? - How to calculate x and y coordinates of a rotated 3D cube in JavaScript? 如何在d3柱形图中的y轴上绘制水平线? - how to draw horizontal line in y axis in d3 column chart?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM