1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
| function generateSinusoidalDXF(length1, numberOfPoints, wavelength, amplitude)
close all; clc;
if nargin < 4 error('Four input arguments required: length1, numberOfPoints, wavelength, and amplitude.'); end
if ~isnumeric(length1) || ~isscalar(length1) || length1 <= 0 error('length1 must be a positive scalar value in millimeters.'); end
if ~isnumeric(numberOfPoints) || ~isscalar(numberOfPoints) || numberOfPoints <= 0 error('numberOfPoints must be a positive integer value.'); end if ~isnumeric(wavelength) || ~isscalar(wavelength) || wavelength <= 0 error('wavelength must be a positive scalar value in millimeters.'); end
if ~isnumeric(amplitude) || ~isscalar(amplitude) error('amplitude must be a numeric scalar value in millimeters.'); end
numberOfPoints = round(numberOfPoints);
waveNumber = 2 * pi / wavelength;
x = linspace(0, length1, numberOfPoints);
y = amplitude * sin(waveNumber * (x + wavelength / 4));
coordinates = [x(:), y(:)];
dxfFileName = sprintf('sinusoidal_curve_%dpts_W%.1f_A%.2f.dxf', ... numberOfPoints, wavelength, amplitude);
writeDXF(dxfFileName, coordinates);
fprintf('DXF file saved as %s\n', dxfFileName); end
function writeDXF(filename, coordinates) fileID = fopen(filename, 'w'); if fileID == -1 error('Failed to open file for writing.'); end
fprintf(fileID, '0\nSECTION\n2\nHEADER\n'); fprintf(fileID, '9\n$INSUNITS\n70\n4\n'); fprintf(fileID, '0\nENDSEC\n');
fprintf(fileID, '0\nSECTION\n2\nTABLES\n0\nENDSEC\n'); fprintf(fileID, '0\nSECTION\n2\nBLOCKS\n0\nENDSEC\n'); fprintf(fileID, '0\nSECTION\n2\nENTITIES\n');
fprintf(fileID, '0\nPOLYLINE\n8\n0\n66\n1\n70\n0\n');
for i = 1:size(coordinates, 1) fprintf(fileID, '0\nVERTEX\n8\n0\n10\n%f\n20\n%f\n', coordinates(i, 1), coordinates(i, 2)); end
fprintf(fileID, '0\nSEQEND\n');
fprintf(fileID, '0\nENDSEC\n0\nEOF\n');
fclose(fileID); end
|