数学部分被理解.与线性编程/线性代数一样,我们可以添加所有较小立方体的拟合体积,以找出最适合较大立方体体积的体积.
实际要求是在网页上以图形方式显示或允许这种拟合,最好是在3d中.如果可能,允许用户与配件交互,即改组立方体的放置等.
此外,由于我是专业的Java开发人员,Java或相关语言/框架将是我的选择.但是,如果最终结果得到满足,我可以使用任何其他技术/框架/语言.
注意:重量也是一个问题(参数).可以在任何给定的存储单元中堆叠最大重量.
此外,由于存储单元可以在未经许可的情况下(通过小偷)访问,因此堆叠在一个单元中的立方体的成本也是有限的.用户可能希望在一个具有更高安全性的单元中安装更高成本的立方体,反之亦然.
示例:允许在给定房间内安装许多包含家用电子设备的矩形盒子.盒子可能是电视,冰箱,洗衣机,洗碗机,游戏机,xBox 360等.这些盒子的不同尺寸,是为了让您了解在适应有限体积时会发生什么.
如果有相同的FOSS库/项目(甚至非FOSS库或项目),欢迎指向它的指针.
解决方法 免责声明:好的,我知道它不是100%回答你的问题,也不是它早期的代码(可以从老式的CVS评论中得出结论),今天我不再这样写了.它仍然在Java 8上运行,但我测试了它.但除了解决水流经长方体3D矩阵从顶部到底部的小信息挑战问题,取决于矩阵(象征某种瑞士奶酪)的“渗漏”,它还通过Java使用一些非常简单的三维可视化3D.因此,您需要安装Java 3D并将相应的库放在类路径中.3D输出看起来像这样:
package vhs.bwinfo.cheese;// $ID: CuboID.java,v 1.1.2.1 2006/01/10 19:48:41 Robin Exp $import javax.media.j3d.Appearance;import javax.media.j3d.QuadArray;import javax.media.j3d.Shape3D;import javax.vecmath.Point3f;import javax.vecmath.TexCoord2f;import javax.vecmath.Vector3f;public class CuboID extends Shape3D { private static final float POS = +0.5f; private static final float NEG = -0.5f; private static final Point3f[] POINTS = new Point3f[] { new Point3f(NEG,NEG,NEG),new Point3f(POS,POS),new Point3f(NEG,POS,POS) }; private static final TexCoord2f[] TEX_COORDS = new TexCoord2f[] { new TexCoord2f(0,1),new TexCoord2f(1,0),new TexCoord2f(0,0) }; private static final int VERTEX_FORMAT = QuadArray.COORDINATES | QuadArray.norMALS | QuadArray.TEXTURE_COORDINATE_2; public CuboID(float scaleX,float scaleY,float scaleZ) { Point3f[] points = new Point3f[8]; for (int i = 0; i < 8; i++) points[i] = new Point3f( POINTS[i].x * scaleX,POINTS[i].y * scaleY,POINTS[i].z * scaleZ ); Point3f[] vertices = { points[3],points[2],points[1],points[0],// bottom points[4],points[5],points[6],points[7],// top points[7],points[3],points[4],// left points[6],// right points[7],// front points[5],points[1] // back }; QuadArray geometry = new QuadArray(24,VERTEX_FORMAT); geometry.setCoordinates(0,vertices); for (int i = 0; i < 24; i++) geometry.setTextureCoordinate(0,i,TEX_COORDS[i % 4]); Vector3f normal = new Vector3f(); Vector3f v1 = new Vector3f(); Vector3f v2 = new Vector3f(); Point3f[] pts = new Point3f[4]; for (int i = 0; i < 4; i++) pts[i] = new Point3f(); for (int face = 0; face < 6; face++) { geometry.getCoordinates(face * 4,pts); v1.sub(pts[0],pts[2]); v2.sub(pts[1],pts[3]); normal.cross(v1,v2); normal.normalize(); for (int i = 0; i < 4; i++) geometry.setnormal((face * 4 + i),normal); } setGeometry(geometry); setAppearance(new Appearance()); } public CuboID(float scaleFactor) { this(scaleFactor,scaleFactor,scaleFactor); }}
package vhs.bwinfo.cheese;// $ID: LeakyCheese.java,v 1.2.2.2 2006/01/10 15:37:14 Robin Exp $import com.sun.j3d.utils.applet.JMainFrame;import javax.swing.*;import java.util.Random;import static java.lang.System.out;public class LeakyCheese { private int wIDth = 20,height = 20,depth = 20; private int numClasses = 100,samplesPerClass = 100; private double pMin = 0,pMax = 1; private double pDiff = pMax - pMin; private double classSize = pDiff / numClasses; private int[] stats; enum CubeState {CHEESE,AIR,WATER} final private CubeState[][][] cheese; private static final Random RND = new Random(); public LeakyCheese( int wIDth,int height,int depth,int numClasses,int samplesPerClass,double pMin,double pMax ) { this.wIDth = wIDth; this.height = height; this.depth = depth; this.numClasses = numClasses; this.samplesPerClass = samplesPerClass; this.pMin = pMin; this.pMax = pMax; pDiff = pMax - pMin; classSize = pDiff / numClasses; cheese = new CubeState[wIDth][height][depth]; } public LeakyCheese( int wIDth,int samplesPerClass ) { this(wIDth,height,depth,numClasses,samplesPerClass,1); } public LeakyCheese() { cheese = new CubeState[wIDth][height][depth]; } private boolean pourWater(int x,int y,int z) { if (x < 0 || x >= wIDth || y < 0 || y >= height || z < 0 || z >= depth) return false; if (cheese[x][y][z] != CubeState.AIR) return false; cheese[x][y][z] = CubeState.WATER; boolean retVal = (y == 0); retVal = pourWater(x + 1,y,z) || retVal; retVal = pourWater(x - 1,z) || retVal; retVal = pourWater(x,y + 1,y - 1,z + 1) || retVal; retVal = pourWater(x,z - 1) || retVal; return retVal; } private boolean isLeaky(double p) { for (int x = 0; x < wIDth; x++) for (int y = 0; y < height; y++) for (int z = 0; z < depth; z++) cheese[x][y][z] = (RND.nextDouble() < p) ? CubeState.CHEESE : CubeState.AIR; boolean retVal = false; for (int x = 0; x < wIDth; x++) for (int z = 0; z < depth; z++) retVal = pourWater(x,height - 1,z) || retVal; return retVal; } private voID generateStats() { if (stats != null) return; stats = new int[numClasses]; for (int i = 0; i < numClasses; i++) { for (int j = 0; j < samplesPerClass; j++) { double p = pMin + classSize * (RND.nextDouble() + i); if (isLeaky(p)) stats[i]++; } } } public voID printStats() { generateStats(); out.println( "p (cheese) | p (leaky)\n" + "------------------+-----------" ); for (int i = 0; i < numClasses; i++) { out.println( String.format( "%1.5f..%1.5f | %1.5f",pMin + classSize * i,pMin + classSize * (i + 1),(double) stats[i] / samplesPerClass ) ); } } public static voID main(String[] args) { //new LeakyCheese().printStats(); //new LeakyCheese(40,40,50,100,0.66,.71).printStats(); LeakyCheese cheeseBlock = new LeakyCheese(5,20,5,100); //LeakyCheese cheeseBlock = new LeakyCheese(20,100); while (!cheeseBlock.isLeaky(0.65)) ; out.println("*** required solution found - Now rendering... ***"); JMainFrame f = new JMainFrame(new LeakyCheeseGUI(cheeseBlock.cheese),512,512); f.setLocationrelativeTo(null); f.setExtendedState(JFrame.MAXIMIZED_BOTH); }}
package vhs.bwinfo.cheese;// $ID: LeakyCheeseGUI.java,v 1.1.2.1 2006/01/10 15:25:18 Robin Exp $import com.sun.j3d.utils.applet.MainFrame;import com.sun.j3d.utils.universe.SimpleUniverse;import vhs.bwinfo.cheese.LeakyCheese.CubeState;import javax.media.j3d.*;import javax.vecmath.Point3d;import javax.vecmath.Vector3f;import java.applet.Applet;import java.awt.*;import java.util.Random;public class LeakyCheeseGUI extends Applet { static final long serialVersionUID = -8194627556699837928L; public BranchGroup createSceneGraph(CubeState[][][] cheese) { // Create the root of the branch graph BranchGroup bgRoot = new BranchGroup(); // Composite of two rotations around different axes. The resulting // transformGroup is the parent of all our cheese cubes,because their // orIEntation is IDentical. They only differ in their translation // values and colours. transform3D tRotate = new transform3D(); transform3D tRotateTemp = new transform3D(); tRotate.rotX(Math.PI / 8.0d); tRotateTemp.rotY(Math.PI / -4.0d); tRotate.mul(tRotateTemp); transformGroup tgRotate = new transformGroup(tRotate); bgRoot.addChild(tgRotate); // Bounding sphere for rendering BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0),100.0); // Set background colour // Note: Using Canvas3D.setBackground does not work,because it is an // AWT method. Java 3D,though,gets its background colour from its // background node (black,if not present). Background background = new Background(0.5f,0.5f,0.5f); background.setApplicationBounds(bounds); bgRoot.addChild(background); TransparencyAttributes transpAttr; // little cheese cubes Appearance cheeseAppearance = new Appearance(); transpAttr = new TransparencyAttributes(TransparencyAttributes.NICEST,0.98f); cheeseAppearance.setTransparencyAttributes(transpAttr); cheeseAppearance.setcoloringAttributes( new coloringAttributes(1,1,coloringAttributes.NICEST)); polygonAttributes pa = new polygonAttributes(); //pa.setpolygonMode(polygonAttributes.polyGON_liNE); pa.setCullFace(polygonAttributes.CulL_NONE); cheeseAppearance.setpolygonAttributes(pa); // little water cubes Appearance waterappearance = new Appearance(); transpAttr = new TransparencyAttributes(TransparencyAttributes.NICEST,0.85f); waterappearance.setTransparencyAttributes(transpAttr); waterappearance.setcoloringAttributes( new coloringAttributes(0,coloringAttributes.NICEST)); pa = new polygonAttributes(); pa.setCullFace(polygonAttributes.CulL_NONE); waterappearance.setpolygonAttributes(pa); // little air cubes Appearance airAppearance = new Appearance(); transpAttr = new TransparencyAttributes(TransparencyAttributes.NICEST,0.95f); airAppearance.setTransparencyAttributes(transpAttr); airAppearance.setcoloringAttributes( new coloringAttributes(1,coloringAttributes.NICEST)); pa = new polygonAttributes(); //pa.setpolygonMode(polygonAttributes.polyGON_liNE); pa.setCullFace(polygonAttributes.CulL_NONE); airAppearance.setpolygonAttributes(pa); // Water-coloured (i.e. blue) wire frame around cheese block,if leaky Appearance waterWireFrameAppearance = new Appearance(); waterWireFrameAppearance.setcoloringAttributes( new coloringAttributes(0,coloringAttributes.NICEST)); pa = new polygonAttributes(); pa.setpolygonMode(polygonAttributes.polyGON_liNE); pa.setCullFace(polygonAttributes.CulL_NONE); waterWireFrameAppearance.setpolygonAttributes(pa); // Cheese-coloured (i.e. yellow) wire frame around cheese block,if not leaky Appearance cheeseWireFrameAppearance = new Appearance(); cheeseWireFrameAppearance.setcoloringAttributes( new coloringAttributes(1,coloringAttributes.NICEST)); pa = new polygonAttributes(); pa.setpolygonMode(polygonAttributes.polyGON_liNE); pa.setCullFace(polygonAttributes.CulL_NONE); cheeseWireFrameAppearance.setpolygonAttributes(pa); // absolute offsets for the cheese block to fit into the vIEwing canvas final float xOffs = -0.8f; final float yOffs = -0.55f; final float zOffs = 0; // Create all those little cubes ;-) final int xSize = cheese.length; final int ySize = cheese[0].length; final int zSize = cheese[0][0].length; final int maxSize = Math.max(xSize,Math.max(ySize,zSize)); final float xCenterOffs = 0.5f * (maxSize - xSize) / maxSize; final float yCenterOffs = 0.5f * (maxSize - ySize) / maxSize; final float zCenterOffs = -0.5f * (maxSize - zSize) / maxSize; boolean isLeaky = false; for (int x = 0; x < xSize; x++) for (int y = 0; y < ySize; y++) for (int z = 0; z < zSize; z++) { transform3D tTranslate = new transform3D(); tTranslate.setTranslation( new Vector3f( xOffs + xCenterOffs + 1.0f * x / maxSize,yOffs + yCenterOffs + 1.0f * y / maxSize,zOffs + zCenterOffs - 1.0f * z / maxSize ) ); transformGroup tgTranslate = new transformGroup(tTranslate); tgRotate.addChild(tgTranslate); CuboID cube = new CuboID(1.0f / maxSize); switch (cheese[x][y][z]) { case CHEESE: cube.setAppearance(cheeseAppearance); break; case WATER: cube.setAppearance(waterappearance); if (y == 0) isLeaky = true; break; case AIR: cube.setAppearance(airAppearance); } tgTranslate.addChild(cube); } // If cheese block is leaky,visualise it by drawing a water-coloured // (i.e. blue) wire frame around it. Otherwise use a cheese-coloured // (i.e. yellow) one. transform3D tTranslate = new transform3D(); tTranslate.setTranslation( new Vector3f( xOffs + xCenterOffs + 0.5f * (xSize - 1) / maxSize,yOffs + yCenterOffs + 0.5f * (ySize - 1) / maxSize,zOffs + zCenterOffs - 0.5f * (zSize - 1) / maxSize ) ); transformGroup tgTranslate = new transformGroup(tTranslate); tgRotate.addChild(tgTranslate); CuboID cuboID = new CuboID( 1.0f * xSize / maxSize,1.0f * ySize / maxSize,1.0f * zSize / maxSize ); cuboID.setAppearance(isLeaky ? waterWireFrameAppearance : cheeseWireFrameAppearance); tgTranslate.addChild(cuboID); // Let Java 3D perform optimizations on this scene graph. bgRoot.compile(); return bgRoot; } public LeakyCheeseGUI(CubeState[][][] cheese) { // Create a simple scene and attach it to the virtual universe GraphicsConfiguration graphCfg = SimpleUniverse.getPreferredConfiguration(); Canvas3D canvas = new Canvas3D(graphCfg); setLayout(new borderLayout()); add(canvas,"Center"); SimpleUniverse universe = new SimpleUniverse(canvas); // This will move the VIEwPlatform back a bit so the objects // in the scene can be vIEwed. universe.getVIEwingPlatform().setNominalVIEwingtransform(); universe.addBranchGraph(createSceneGraph(cheese)); } public static voID main(String[] args) { final Random RND = new Random(System.currentTimeMillis()); CubeState[][][] testCheese = new CubeState[5][8][11]; for (int x = 0; x < 5; x++) for (int y = 0; y < 8; y++) for (int z = 0; z < 11; z++) testCheese[x][y][z] = (RND.nextfloat() < 0.7f) ? CubeState.CHEESE : (RND.nextBoolean() ? CubeState.WATER : CubeState.AIR); // Applet can also run as a stand-alone application new MainFrame(new LeakyCheeseGUI(testCheese),512); }}总结
以上是内存溢出为你收集整理的如何将小方块放入给定的卷并在网页上以图形方式表示?全部内容,希望文章能够帮你解决如何将小方块放入给定的卷并在网页上以图形方式表示?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)