Skip to content

Instantly share code, notes, and snippets.

@hmparanjape
Created August 10, 2015 01:14
Show Gist options
  • Save hmparanjape/d12eef7f489511bb06fa to your computer and use it in GitHub Desktop.
Save hmparanjape/d12eef7f489511bb06fa to your computer and use it in GitHub Desktop.
A MATLAB script to create 2D line patterns inspired by far-field high energy x-ray diffraction.
%% Moving diffraction patterns
% A MATLAB-based program to generate high energy x-ray diffraction inspired
% line art.
%
%% USAGE
% Enter various parameters in the INPUT section. The meaning of each
% parameter is described below. Then run this script. It will produce a
% plot with the line patterns.
%
% The basic concept behind the pattern generation is this -
% We start with a few spots in a plne, much like an area diffraction
% pattern generated during synchrotron-based far-field x-ray diffraction.
% Then we move the detector. There are several degrees of freedom possible
% - translation perpendicular to the detector plane, rotation along the
% detector plane normal, translation along the detector width and height.
% Additionally, a purturbation can be applied to the ngular and radial
% position of a spot.
% This can produce a rich variety of line patterns. You can run the script
% with the default values to see an example. For many parameters, a few
% examples are given.
%
%% Inputs
number_of_spots = 200; % Number of spots. More = denser pattern
seed_distribution = 1; % 0 = rand, 1 = uniform
d_seed = 0.1; % Start detector distance (normalized units)
detector_angular_velocity = 0.0; % angular velo (normalized units)
detector_normal_velocity = 4; % velo normal to detctor plane (normalized units)
detector_x_velocity = @(x, y, s) 0; % detector speed along X axis
% Examples:
% 3.5*sin(x/300*pi/2);
detector_y_velocity = @(x, y, s) 5; % detector speed along y axis
% Examples:
% 2*sin((x)/300*pi/2.1);
total_time = 10; % Total time for movement
time_steps = 300; % Steps
plot_line_width = 8; % Width of a line in the plot
debug_mode = 0; % 1 = display debug info
% dev functions: purturbe the angles or the radial diatance of each spot
angular_dev_function = @(x, y, s) sin(x/time_steps*pi*10)*0.1;
% Examples
% sign(mod(floor(x/50), 2) - 2)*tan(mod(x, 50)/55*pi/2.2)*0.1;
normal_dev_function = @(x, y, s) 0;
% Examples
% @(x, y, s) tan(x/time_steps*pi/2.03)*0.4;
% @(x, y, s) sin(20*s)*0.4;
% @(x, y, s) sawtooth(20*s, 0.5)*1.6*x/time_steps;
% @(x, y, s) min(abs(tan(20*s)*1.6*x/time_steps), 5);
%
% END of inputs
%
if(seed_distribution == 0)
eta_seed = rand(number_of_spots,1)*2.0*pi; % Start angular positions for spots (rad)
elseif(seed_distribution == 1)
eta_seed = [0:(2*pi/number_of_spots):(2*pi)]';
end
spot_location_seed = [d_seed*cos(eta_seed) d_seed*sin(eta_seed)]; % X, Y coordinates for starting spot positions
%
spot_location = zeros(size(spot_location_seed, 1), time_steps, 2);
eta = zeros(size(eta_seed, 1), 1);
% Loop over time steps
for i = 1:time_steps
d = d_seed + total_time/time_steps*detector_normal_velocity*i;
for j = 1:size(eta_seed, 1)
eta(j) = eta_seed(j) + total_time/time_steps*detector_angular_velocity*i + angular_dev_function(i, j, d);
% Update coordinates of the spot
spot_location(j, i, :) = [(d + normal_dev_function(i, j, eta(j)))*cos(eta(j)) + detector_x_velocity(i, j, eta(j))*total_time/time_steps*i
(d + normal_dev_function(i, j, eta(j)))*sin(eta(j)) + detector_y_velocity(i, j, eta(j))*total_time/time_steps*i];
if(debug_mode)
disp([d normal_dev_function(i, j, d)])
end
end
end
%
% Plot
f = figure;
set(f, 'Units', 'Normalized')
set(f, 'Position', [0.1, 0.1, 0.8, 0.8]);
hold on;
spot_color = rand(size(eta_seed, 1), 3); %hsv(size(eta_seed, 1));
for i = 1:size(eta_seed, 1)
plot(squeeze(spot_location(i, :, 1)), squeeze(spot_location(i, :, 2)), 'LineWidth', plot_line_width, 'Color', spot_color(i, :))
end
hold off;
axis equal;
axis off;
%
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment