水晶球怎么画里面有两种颜色的颗粒

Posted

技术标签:

【中文标题】水晶球怎么画里面有两种颜色的颗粒【英文标题】:How to draw a crystal ball with two-color particles inside 【发布时间】:2012-10-13 13:43:59 【问题描述】:

我只是提出一个可能关闭的想法。我需要画一个水晶球,其中红色和蓝色粒子随机分布。我想我必须使用 Photoshop,甚至尝试在图像中制作球,但由于这是用于研究论文并且不必花哨,我想知道是否有任何方法可以使用 R、matlab 或任何其他语言。

【问题讨论】:

我会使用VMD 来完成这样的任务,但无论如何我每天都会使用它,对于初学者来说可能很难...... 是不是应该用这个水晶球来预测这个球的样子?您是否想到了特定的几何形状/对象?继续做一些事情,即使是在餐巾纸背面的涂鸦,被你的手机相机抓住并上传到这里都会有所帮助。或者打开一个图像编辑,徒手粗略地画出来,保存并上传到这里。 @bla:你能告诉我们你在寻找什么在当前的答案集中没有找到/做得不够好吗? 不,我不能,我只是想把我的代表点花在我喜欢看的东西上(更多答案\答案选项)。如果没有任何令人兴奋的事情发生,我会为我最喜欢的答案提供赏金。有什么不对吗? 不......只是想知道我们是否有特别的目标。 【参考方案1】:

在 R 中,使用 rgl 包(R-to-OpenGL 接口):

library(rgl)
n <- 100
set.seed(101)
randcoord <- function(n=100,r=1) 
    d <- data.frame(rho=runif(n)*r,phi=runif(n)*2*pi,psi=runif(n)*2*pi)
    with(d,data.frame(x=rho*sin(phi)*cos(psi),
                      y=rho*sin(phi)*sin(psi),
                      z=rho*cos(phi)))

    ## http://en.wikipedia.org/wiki/List_of_common_coordinate_transformations
with(randcoord(50,r=0.95),spheres3d(x,y,z,radius=0.02,col="red"))
with(randcoord(50,r=0.95),spheres3d(x,y,z,radius=0.02,col="blue"))
spheres3d(0,0,0,radius=1,col="white",alpha=0.5,shininess=128)
rgl.bg(col="black")
rgl.snapshot("crystalball.png")

【讨论】:

这是一个非常漂亮的水晶球,但它的底部似乎有点漏水。 :-) 是的,我认为设置粒子位置的最大半径可以解决这个问题,但似乎没有。我应该多玩。 您使用的语言是什么? 只是我,还是那些粒子似乎在球内移动/摆动?一开始我以为是动画,结果只是个png……一定是某种视错觉。【参考方案2】:

这与 Ben Bolker 的回答非常相似,但我正在演示如何通过使用一些神秘的颜色为水晶球添加一点光环:

library(rgl)
lapply(seq(0.01, 1, by=0.01), function(x) rgl.spheres(0,0,0, rad=1.1*x, alpha=.01,
    col=colorRampPalette(c("orange","blue"))(100)[100*x]))
rgl.spheres(0,0,0, radius=1.11, col="red", alpha=.1)
rgl.spheres(0,0,0, radius=1.12, col="black", alpha=.1)
rgl.spheres(0,0,0, radius=1.13, col="white", alpha=.1)

xyz <- matrix(rnorm(3*100), ncol=3)
xyz <- xyz * runif(100)^(1/3) / sqrt(rowSums(xyz^2))

rgl.spheres(xyz[1:50,], rad=.02, col="blue")
rgl.spheres(xyz[51:100,], rad=.02, col="red")

rgl.bg(col="black")
rgl.viewpoint(zoom=.75)
rgl.snapshot("crystalball.png")

两者之间的唯一区别在于lapply 调用。您可以看到,只需更改colorRampPalette 中的颜色,您就可以显着改变水晶球的外观。左边的使用上面的lapply 代码,右边的使用这个代码:

lapply(seq(0.01, 1, by=0.01), function(x) rgl.spheres(0,0,0,rad=1.1*x, alpha=.01,
     col=colorRampPalette(c("orange","yellow"))(100)[100*x]))
...code from above

这是一种不同的方法,您可以定义自己的纹理文件并使用它为水晶球着色:

# create a texture file, get as creative as you want:
png("texture.png")
x <- seq(1,870)
y <- seq(1,610)
z <- matrix(rnorm(870*610), nrow=870)
z <- t(apply(z,1,cumsum))/100

# Swirly texture options:
# Use the Simon O'Hanlon's roll function from this answer:
# http://***.com/questions/18791212/equivalent-to-numpy-roll-in-r/18791252#18791252
# roll <- function( x , n )
#   if( n == 0 )
#     return( x )
#   c( tail(x,n) , head(x,-n) )
# 

# One option
# z <- mapply(function(x,y) roll(z[,x], y), x = 1:ncol(z), y=1:ncol(z))
#
# Another option
# z <- mapply(function(x,y) roll(z[,x], y), x = 1:ncol(z), y=rep(c(1:50,51:2), 10))[1:870, 1:610]
#
# One more
# z <- mapply(function(x,y) roll(z[,x], y), x = 1:ncol(z), y=rep(seq(0, 100, by=10), each=5))[1:870, 1:610]

par(mar=c(0,0,0,0))
image(x, y, z, col = colorRampPalette(c("cyan","black"))(100), axes = FALSE)
dev.off()

xyz <- matrix(rnorm(3*100), ncol=3)
xyz <- xyz * runif(100)^(1/3) / sqrt(rowSums(xyz^2))

rgl.spheres(xyz[1:50,], rad=.02, col="blue")
rgl.spheres(xyz[51:100,], rad=.02, col="red")

rgl.spheres(0,0,0, rad=1.1, texture="texture.png", alpha=0.4, back="cull")
rgl.viewpoint(phi=90, zoom=.75) # change the view if need be
rgl.bg(color="black")

!

左上角的第一张图是你运行上面的代码得到的,其他三张是在注释掉的代码中使用不同选项的结果。

【讨论】:

这些真的很壮观! 只是一个小评论:使用 spheres3d() 而不是 rgl.spheres() 可能会更好。 rgl.* 函数以一种奇怪的方式处理颜色等的默认值。【参考方案3】:

问题是

我想知道是否有任何方法可以使用 R、matlab 或任何其他语言进行编程。

并且 TeX 是图灵完备的,可以被认为是一种编程语言,我花了一些时间并使用 TikZ 在 LaTeX 中创建了一个示例。正如 OP 所写的那样,它是针对研究论文的,假设它也是用 LaTeX 编写的,它具有可以直接集成到论文中的优点。

所以,这里是:

\documentclass[tikz]standalone
\usetikzlibrarypositioning, backgrounds
\usepackagepgf
\pgfmathsetseed\number\pdfrandomseed

\begindocument
\begintikzpicture[background rectangle/.style=fill=black,
                    show background rectangle,
                   ] 

    % Definitions
    \def\ballRadius5
    \def\pointRadius0.1
    \def\nRed30
    \def\nBlue30

    % Draw all red points
    \foreach \i in 1,...,\nRed
    
        % Get random coordinates
        \pgfmathparse0.9*\ballRadius*rand\let\mrho\pgfmathresult
        \pgfmathparse360*rand\let\mpsi\pgfmathresult
        \pgfmathparse360*rand\let\mphi\pgfmathresult

        % Convert to x/y/z
        \pgfmathparse\mrho*sin(\mphi)*cos(\mpsi)\let\mx\pgfmathresult
        \pgfmathparse\mrho*sin(\mphi)*sin(\mpsi)\let\my\pgfmathresult
        \pgfmathparse\mrho*cos(\mphi)\let\mz\pgfmathresult

        \fill[ball color=blue] (\mz,\mx,\my) circle (\pointRadius);
    

    % Draw all blue points
    \foreach \i in 1,...,\nBlue
    
        % Get random coordinates
        \pgfmathparse0.9*\ballRadius*rand\let\mrho\pgfmathresult
        \pgfmathparse360*rand\let\mpsi\pgfmathresult
        \pgfmathparse360*rand\let\mphi\pgfmathresult

        % Convert to x/y/z
        \pgfmathparse\mrho*sin(\mphi)*cos(\mpsi)\let\mx\pgfmathresult
        \pgfmathparse\mrho*sin(\mphi)*sin(\mpsi)\let\my\pgfmathresult
        \pgfmathparse\mrho*cos(\mphi)\let\mz\pgfmathresult

        \fill[ball color=red] (\mz,\mx,\my) circle (\pointRadius);
    

    % Draw ball
    \shade[ball color=blue!10!white,opacity=0.65] (0,0) circle (\ballRadius);

\endtikzpicture
\enddocument

结果:

【讨论】:

【参考方案4】:

我只是不得不在 Matlab 中生成与 R 答案一样闪亮的东西 :) 所以,这是我深夜的、过于复杂、超慢的解决方案,但是 我的 很漂亮不是吗? :)

figure(1), clf, hold on
whitebg('k')    

light(...
    'Color','w',...
    'Position',[-3 -1 0],...
    'Style','infinite')

colormap cool
brighten(0.2)

[x,y,z] = sphere(50);
surf(x,y,z);

lighting phong
alpha(.2)
shading interp
grid off

blues = 2*rand(15,3)-1;
reds  = 2*rand(15,3)-1;
R     = linspace(0.001, 0.02, 20);

done = false;
while ~done

    indsB = sum(blues.^2,2)>1-0.02;    
    if any(indsB)
        done = false;
        blues(indsB,:) = 2*rand(sum(indsB),3)-1; 
    else
        done = true;
    end

    indsR = sum( reds.^2,2)>1-0.02;
    if any(indsR)
        done = false;
        reds(indsR,:) = 2*rand(sum(indsR),3)-1; 
    else
        done = done && true;
    end

end

nR = numel(R);
[x,y,z] = sphere(15);
for ii = 1:size(blues,1)
    for jj = 1:nR        
        surf(x*R(jj)-blues(ii,1), y*R(jj)-blues(ii,2), z*R(jj)-blues(ii,3), ...
            'edgecolor', 'none', ...
            'facecolor', [1-jj/nR 1-jj/nR 1],...
            'facealpha', exp(-(jj-1)/5));
    end
end

nR = numel(R);
[x,y,z] = sphere(15);
for ii = 1:size(reds,1)
    for jj = 1:nR        
        surf(x*R(jj)-reds(ii,1), y*R(jj)-reds(ii,2), z*R(jj)-reds(ii,3), ...
            'edgecolor', 'none', ...
            'facecolor', [1 1-jj/nR 1-jj/nR],...
            'facealpha', exp(-(jj-1)/5));
    end
end

set(findobj(gca,'type','surface'),...
    'FaceLighting','phong',...
    'SpecularStrength',1,...
    'DiffuseStrength',0.6,...
    'AmbientStrength',0.9,...
    'SpecularExponent',200,...
    'SpecularColorReflectance',0.4 ,...
    'BackFaceLighting','lit');

axis equal
view(30,60)

【讨论】:

不知道你为什么提到它超级慢,运行只需要大约一秒钟,这对于想要制作一张照片的人来说似乎很好。 是的。我只知道当更加关注这一点时,它可能会快一个数量级。但事实上,谁在乎 :)【参考方案5】:

我建议你看看ray-tracing program、for instancepovray。我不太懂这门语言,但摆弄了一些例子,我不费吹灰之力就完成了这个。

background  color rgb <1,1,1,1> 
#include "colors.inc"
#include "glass.inc" 

#declare R = 3;
#declare Rs = 0.05;
#declare Rd = R - Rs ;

camera location <1, 10 ,1>
right <0, 4/3, 0>
 up    <0,0.1,1>
 look_at  <0.0 , 0.0 , 0.0>

light_source  
    z*10000
    White
    

light_source<15,25,-25> color  rgb <1,1,1> 

#declare T_05 = texture  pigment  color Clear  finish  F_Glass1   


#declare Ball = sphere 
    <0,0,0>, R
      pigment  rgbf <0.75,0.8,1,0.9>  // A blue-tinted glass

    finish
   phong 0.5 phong_size 40  // A highlight
    reflection 0.2  // Glass reflects a bit
  
    interiorior 1.5
  

#declare redsphere =    sphere 
    <0,0,0>, Rs
        pigmentcolor Red
      texture  T_05  interior  I_Glass4 fade_color Col_Red_01 

#declare bluesphere =   sphere 
    <0,0,0>, Rs
    pigmentcolor Blue
      texture  T_05  interior  I_Glass4 fade_color Col_Blue_01 

object Ball 

#declare Rnd_1 = seed (123);
 #for (Cntr, 0, 200)
#declare rr = Rd* rand( Rnd_1);
#declare theta = -pi/2 + pi * rand( Rnd_1);
#declare phi = -pi+2*pi* rand( Rnd_1);
#declare xx = rr * cos(theta) * cos(phi);
#declare yy = rr * cos(theta) * sin(phi);
#declare zz = rr * sin(theta) ;
object bluesphere  translate  <xx , yy , zz > 
#declare rr = Rd* rand( Rnd_1);
#declare theta = -pi/2 + pi * rand( Rnd_1);
#declare phi = -pi+2*pi* rand( Rnd_1);
#declare xx = rr * cos(theta) * cos(phi);
#declare yy = rr * cos(theta) * sin(phi);
#declare zz = rr * sin(theta) ;
object redsphere  translate  <xx , yy , zz > 
#end 

【讨论】:

非常漂亮的水晶球,povray 的灯光效果很好 你能把代码贴在这里吗?有点长,但似乎不太长。【参考方案6】:

游戏有点晚了,但这里有一个实现scatter3sph(来自 FEX)的 Matlab 代码

figure('Color', [0.04 0.15 0.4]);
nos = 11; % number small of spheres
S= 3; %small spheres sizes
Grid_Size=256;
%Coordinates
X= Grid_Size*(0.5+rand(2*nos,1));
Y= Grid_Size*(0.5+rand(2*nos,1));
Z= Grid_Size*(0.5+rand(2*nos,1));
%Small spheres colors: (Red & Blue)
C= ones(nos,1)*[0 0 1];
C= [C;ones(nos,1)*[1 0 0]];
% Plot big Sphere
scatter3sph(Grid_Size,Grid_Size,Grid_Size,'size',220,'color',[0.9 0.9 0.9]); hold on
light('Position',[0 0 0],'Style','local');
alpha(0.45);
material shiny 
% Plot small spheres 
scatter3sph(X,Y,Z,'size',S,'color',C);  
axis equal; axis tight; grid off
view([108 -42]);
set(gca,'Visible','off')
set(gca,'color','none')

【讨论】:

不错。我觉得应该可以在 R 示例中获得光泽,但我不想乱搞。【参考方案7】:

使用 Matlab 的另一种解决方案。

[x,y,z] = sphere(50);
[img] = imread('crystal.jpg');

figure('Color',[0 0 0]);
surf(x,y,z,img,'edgeColor','none','FaceAlpha',.6,'FaceColor','texturemap')
hold on;

i = 0;
while i<100
    px = randn();
    py = randn();
    pz = randn();
    d = pdist([0 0 0; px py pz],'euclidean');
    if d<1
        if mod(i,2)==0
            scatter3(px, py, pz,30,'ro','filled');
        else
            scatter3(px, py, pz,30,'bo','filled');
        end
        i = i+1;
    end
end

hold off;
camlight;

axis equal;
axis off;

输出:

【讨论】:

【参考方案8】:

在带有 d3.js 的 javascript 中:http://jsfiddle.net/jjcosare/rggn86aj/6/ 或 > 运行代码片段

对在线发布很有用。

var particleChangePerMs = 1000;
var particleTotal = 250;
var particleSizeInRelationToCircle = 75;

var svgWidth = (window.innerWidth > window.innerHeight) ? window.innerHeight : window.innerWidth;
var svgHeight = (window.innerHeight > window.innerWidth) ? window.innerWidth : window.innerHeight;

var circleX = svgWidth / 2;
var circleY = svgHeight / 2;
var circleRadius = (circleX / 4) + (circleY / 4);
var circleDiameter = circleRadius * 2;

var particleX = function() 
  return Math.floor(Math.random() * circleDiameter) + circleX - circleRadius;
;
var particleY = function() 
  return Math.floor(Math.random() * circleDiameter) + circleY - circleRadius;
;
var particleRadius = function() 
  return circleDiameter / particleSizeInRelationToCircle;
;
var particleColorList = [
  'blue',
  'red'
];
var particleColor = function() 
  return "url(#" + particleColorList[Math.floor(Math.random() * particleColorList.length)] + "Gradient)";
;

var svg = d3.select("#quantumBall")
  .append("svg")
  .attr("width", svgWidth)
  .attr("height", svgHeight);

var blackGradient = svg.append("svg:defs")
  .append("svg:radialGradient")
  .attr("id", "blackGradient")
  .attr("cx", "50%")
  .attr("cy", "50%")
  .attr("radius", "90%")

blackGradient.append("svg:stop")
  .attr("offset", "80%")
  .attr("stop-color", "black")

blackGradient.append("svg:stop")
  .attr("offset", "100%")
  .attr("stop-color", "grey")

var redGradient = svg.append("svg:defs")
  .append("svg:linearGradient")
  .attr("id", "redGradient")
  .attr("x1", "0%")
  .attr("y1", "0%")
  .attr("x2", "100%")
  .attr("y2", "100%")
  .attr("spreadMethod", "pad");

redGradient.append("svg:stop")
  .attr("offset", "0%")
  .attr("stop-color", "red")
  .attr("stop-opacity", 1);

redGradient.append("svg:stop")
  .attr("offset", "100%")
  .attr("stop-color", "pink")
  .attr("stop-opacity", 1);

var blueGradient = svg.append("svg:defs")
  .append("svg:linearGradient")
  .attr("id", "blueGradient")
  .attr("x1", "0%")
  .attr("y1", "0%")
  .attr("x2", "100%")
  .attr("y2", "100%")
  .attr("spreadMethod", "pad");

blueGradient.append("svg:stop")
  .attr("offset", "0%")
  .attr("stop-color", "blue")
  .attr("stop-opacity", 1);

blueGradient.append("svg:stop")
  .attr("offset", "100%")
  .attr("stop-color", "skyblue")
  .attr("stop-opacity", 1);

svg.append("circle")
  .attr("r", circleRadius)
  .attr("cx", circleX)
  .attr("cy", circleY)
  .attr("fill", "url(#blackGradient)");

function isParticleInQuantumBall(particle) 
  var x1 = circleX;
  var y1 = circleY;
  var r1 = circleRadius;
  var x0 = particle.x;
  var y0 = particle.y;
  var r0 = particle.radius;
  return Math.sqrt((x1 - x0) * (x1 - x0) + (y1 - y0) * (y1 - y0)) < (r1 - r0);
;

function randomizedParticles() 
  d3.selectAll("svg > .particle").remove();
  var particle = ;
  particle.radius = particleRadius();
  for (var i = 0; i < particleTotal;) 
    particle.x = particleX();
    particle.y = particleY();
    particle.color = particleColor();
    if (isParticleInQuantumBall(particle)) 
      svg.append("circle")
        .attr("class", "particle")
        .attr("cx", particle.x)
        .attr("cy", particle.y)
        .attr("r", particle.radius)
        .attr("fill", particle.color);
      i++;
    
  


setInterval(randomizedParticles, particleChangePerMs);
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<div id="quantumBall"></div>

【讨论】:

【参考方案9】:

在 R 中,您可以使用 rasterImage 函数添加到当前绘图,您可以创建/下载漂亮的水晶球图像并将其加载到 R 中(参见 png、EBImage 或其他包),然后制作它是半透明的并使用rasterImage 将其添加到当前绘图中。我可能会先绘制你的 2 个彩色点,然后在顶部绘制球的图像(在透明度的情况下,它们仍然可见并且看起来就像它们在里面)。

一种更简单的方法(虽然可能没有那么好看)是使用polygon 函数绘制一个半透明的灰色圆圈来表示球。

如果您想在 3 维中执行此操作,请查看 rgl 包,这是一个基本示例:

library(rgl)
open3d()
spheres3d(0,0,0, radius=1, color='lightgrey', alpha=0.2)
spheres3d(c(.3,-.3),c(-.2,.4),c(.1,.2), color=c('red','blue'),
     alpha=1, radius=0.15)

【讨论】:

以上是关于水晶球怎么画里面有两种颜色的颗粒的主要内容,如果未能解决你的问题,请参考以下文章

各种颜色的水晶用英文单词怎么说?

idea里面代码怎么复制后带颜色啊?

css画长方形颜色各一半怎么画

css3画矩形,矩形里面有多个圆圈,圆圈里面有字,如何实现?

Opencv画图函数整理 及 cvCircle cvLine 只能画出黑白两种颜色问题 解决

matlab 设置曲线颜色