java绘制一个球体_html5 – 如何绘制3D球体?

更新:此代码相当老旧,有限.现在有三维3D球体库:

http://techslides.com/d3-globe-with-canvas-webgl-and-three-js/

十年前,我写了一个Java小程序来渲染一个纹理的球体,通过实际做数学来计算球体表面在场景中的位置(不使用三角形).

我的机器上有22 fps哪个是基于渲染的Java版本一样快,如果不是更快一点!

现在,我写了Java代码已经很久了 – 而且它是相当钝的 – 所以我真的不记得它是如何工作的,我刚刚移植了JavaScript.然而,这是从缓慢版本的代码,我不知道如果更快的版本是由于Java方法中的优化,我用来操纵像素或加速在数学中,从而制定出哪些像素从质地.我当时也是相应的,有一个类似applet的人比我的速度要快,但是我不知道在JavaScript中是否有可能提升速度,因为它可能依赖于Java库. (我从来没有看过他们的代码,所以我不知道他们是怎么做到的)

所以可能会提高速度.但这是一个很好的概念证明.

如果您有兴趣比较速度,Java版本在这里:

这些是死链接,直到我下一次更新我的网站

我会去转换我的更快的版本一段时间,看看我是否可以改进JavaScript版本的任何速度.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java实现一个模型、两个视图和两个控制器的功能软件,即采用MVC模式或者说是观察者模式,本程序通过输入球体半径,显示球体形状,面积体积等 Sphere.java package Model; import java.util.Observable; public class Sphere extends Observable { private double radius;//球体半径 private double area;//球体面积 private double volume;//球体体积 public Sphere() { radius=100d; area=4*Math.PI*Math.pow(radius, 2); volume=4*Math.PI*Math.pow(radius, 3)/3; } public double getRadius() { return radius; } public double getArea() { return area; } public double getVolume() { return volume; } public void setRadius(double radius) { this.radius = radius; this.area = 4*Math.PI*Math.pow(radius, 2); this.volume=4*Math.PI*Math.pow(radius, 3)/3; this.setChanged(); this.notifyObservers(); } } textView.java package View; import java.util.Observer; import java.util.Observable; import java.text.NumberFormat; import javax.swing.*; import Controller.TextController; import Model.Sphere; import java.awt.*; import java.awt.event.*; public class TextView extends JPanel implements Observer { private JLabel radiusLab;//提示用户输入球体半径 private JTextField radiusTextField;//接受用户输入球体半径 //private JLabel radiusRang; private JLabel areaLab;//显示球体面积 private JTextField areaTextField;//显示输入球体半径对应的面积 private JLabel volumeLab;//显示球体体积 private JTextField volumeTextField;//显示输入球体半径对应的体积 public TextView() { try { Init(); } catch(Exception e) { e.printStackTrace(); } } private void Init() throws Exception { radiusLab=new JLabel("球体半径"); radiusLab.setForeground(new Color(0,165,168)); //radiusRang=new JLabel("[0-200]"); radiusTextField = new JTextField(12); radiusTextField.setForeground(new Color(223,100,158)); radiusTextField.setBackground(new Color(210,204,230)); areaLab=new JLabel("球体面积"); areaLab.setForeground(new Color(0,165,168)); areaTextField = new JTextField(12); areaTextField.setBackground(new Color(193,219,219)); areaTextField.setEditable(fal
以下是一个使用Java实现的纯白色球体的光照模型代码示例: ```java import java.awt.Color; import java.awt.Graphics; import javax.swing.JFrame; import javax.swing.JPanel; public class WhiteSphere extends JPanel { private static final long serialVersionUID = 1L; private int x = 100; private int y = 100; private int z = 100; private int radius = 50; private float light_intensity = 1.0f; private float light_position[] = { 0.0f, 0.0f, 1.0f }; public void paint(Graphics g) { super.paint(g); // 绘制白色球体 g.setColor(Color.WHITE); int diameter = radius * 2; g.fillOval(x - radius, y - radius, diameter, diameter); // 计算球体表面的光照强度 float[] normal = { x - light_position[0], y - light_position[1], z - light_position[2] }; float len = (float) Math.sqrt(normal[0] * normal[0] + normal[1] * normal[1] + normal[2] * normal[2]); normal[0] /= len; normal[1] /= len; normal[2] /= len; float[] light_dir = { light_position[0] - x, light_position[1] - y, light_position[2] - z }; len = (float) Math.sqrt(light_dir[0] * light_dir[0] + light_dir[1] * light_dir[1] + light_dir[2] * light_dir[2]); light_dir[0] /= len; light_dir[1] /= len; light_dir[2] /= len; float intensity = normal[0] * light_dir[0] + normal[1] * light_dir[1] + normal[2] * light_dir[2]; if (intensity < 0) { intensity = 0; } // 根据光照强度调整球体颜色 Color c = new Color((int) (255 * light_intensity * intensity), (int) (255 * light_intensity * intensity), (int) (255 * light_intensity * intensity)); g.setColor(c); g.fillOval(x - radius, y - radius, diameter, diameter); } public static void main(String[] args) { JFrame frame = new JFrame(); frame.setSize(400, 400); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); WhiteSphere panel = new WhiteSphere(); frame.add(panel); frame.setVisible(true); } } ``` 在上面的代码中,我们创建了一个WhiteSphere类来绘制一个纯白色的球体,并实现了简单的光照模型。在paint()方法中,我们首先绘制一个纯白色的球体,然后计算球体表面的光照强度,根据光照强度调整球体的颜色。光照强度的计算使用了向量内积的方法,光照的位置和强度可以通过修改light_position和light_intensity变量来调整。 运行上面的代码,可以得到一个简单的纯白球体,并可以通过修改光照位置和强度来调整球体的颜色和光照效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值