function [ fraction ] = Solid_volume_fraction_fast_algorithm(sim,part,near_line,point,partnum)

size = sim.meshsize/2;
corner = [ point(1,1)-size point(1,1)+size point(1,1)+size point(1,1)-size point(1,1)-size
    point(2,1)-size point(2,1)-size point(2,1)+size point(2,1)+size point(2,1)-size];
corner_num = part.corner_num(partnum);
center = point;
special_record = zeros(2,2);
special_record_i = 0;

matrix = zeros(3,25);
% 1-intersection or not; 2-x of interpoint; 3-y interpoint
matrix([1,1,1,1,1],[1,7,13,19,25]) = 1;
matrix(2:3,[1,7,13,19,25]) = corner;

if near_line==0
    lines = part.corner_pos(:,1:corner_num+1,partnum);
    lines_num = corner_num;
else
    lines = zeros(2,4);
    lines_num = 3;
    lines(:,2:3) = part.corner_pos(:,near_line:near_line+1,partnum);
    if near_line==1
        lines(:,1) = part.corner_pos(:,corner_num,partnum);
    else
        lines(:,1) = part.corner_pos(:,near_line-1,partnum);
    end
    if near_line==corner_num
        lines(:,4) = part.corner_pos(:,2,partnum);
    else
        lines(:,4) = part.corner_pos(:,near_line+2,partnum);
    end
end

%i:1-4 square edge; ii:1-lines_num
loop_jump = zeros(1,lines_num);
i=1;
for ii=1:lines_num
    inter = intersection(lines(:,ii),lines(:,ii+1),corner(:,i),corner(:,i+1));
    if inter == 1
        loop_jump(ii) = 1;
        inter_point = intersection_point...
            (lines(:,ii),lines(:,ii+1),corner(:,i),corner(:,i+1));
        [matrix] = insert_line(matrix,inter_point,i);%last number:insert slot
        i2 = 2;
        inter2 = intersection(lines(:,ii),lines(:,ii+1),corner(:,i2),corner(:,i2+1));
        if inter2 == 1
            matrix(1,7) = 0;%delete point
            inter_point2 = intersection_point(lines(:,ii),lines(:,ii+1),corner(:,i2),corner(:,i2+1));
            [matrix] = insert_line(matrix,inter_point2,i2);%last number:insert slot
        else
            i2 = 4;
            inter2 = intersection(lines(:,ii),lines(:,ii+1),corner(:,i2),corner(:,i2+1));
            if inter2 == 1
                matrix(1,1) = 0;%delete point
                inter_point2 = intersection_point(lines(:,ii),lines(:,ii+1),corner(:,i2),corner(:,i2+1));
                [matrix] = insert_line(matrix,inter_point2,i2);%last number:insert slot
            else
                %face to face line
                i2 = 3;
                inter2 = intersection(lines(:,ii),lines(:,ii+1),corner(:,i2),corner(:,i2+1));
                if inter2 == 1
                    inter_point2 = intersection_point(lines(:,ii),lines(:,ii+1),corner(:,i2),corner(:,i2+1));
                    [matrix] = insert_line(matrix,inter_point2,i2);%last number:insert slot
                    %judge intersection c2&line
                    judge_c = intersection(lines(:,ii),lines(:,ii+1),center,corner(:,2));
                    if judge_c==1
                        matrix(1,7) = 0;%delete point
                        matrix(1,13) = 0;%delete point
                    else
                        matrix(1,1) = 0;%delete point
                        matrix(1,19) = 0;%delete point
                    end
                else
                    %only intersect with edge 1
                    special_record_i = special_record_i+1;
                    special_record(special_record_i,:) = [1,ii]; %first number:code. second number:line
                end
            end
        end
    end
end

i=2;
for ii=1:lines_num
    if loop_jump(ii) == 1
        continue
    end
    inter = intersection(lines(:,ii),lines(:,ii+1),corner(:,i),corner(:,i+1));
    if inter == 1
        loop_jump(ii) = 1;
        inter_point = intersection_point...
            (lines(:,ii),lines(:,ii+1),corner(:,i),corner(:,i+1));
        [matrix] = insert_line(matrix,inter_point,i);%last number:insert slot
        i2 = 3;
        inter2 = intersection(lines(:,ii),lines(:,ii+1),corner(:,i2),corner(:,i2+1));
        if inter2 == 1
            matrix(1,13) = 0;%delete point 3
            inter_point2 = intersection_point(lines(:,ii),lines(:,ii+1),corner(:,i2),corner(:,i2+1));
            [matrix] = insert_line(matrix,inter_point2,i2);%last number:insert slot
        else
            %face to face line
            i2 = 4;
            inter2 = intersection(lines(:,ii),lines(:,ii+1),corner(:,i2),corner(:,i2+1));
            if inter2 == 1
                inter_point2 = intersection_point(lines(:,ii),lines(:,ii+1),corner(:,i2),corner(:,i2+1));
                [matrix] = insert_line(matrix,inter_point2,i2);%last number:insert slot
                %judge intersection c2&line
                judge_c = intersection(lines(:,ii),lines(:,ii+1),center,corner(:,2));
                if judge_c==1
                    matrix(1,1) = 0;%delete point 1
                    matrix(1,7) = 0;%delete point 2
                else
                    matrix(1,13) = 0;%delete point 3
                    matrix(1,19) = 0;%delete point 4
                end
            else
                %only intersect with edge 1
                special_record_i = special_record_i+1;
                special_record(special_record_i,:) = [2,ii]; %first number:code. second number:line
            end
        end
    end
end

i=3;
for ii=1:lines_num
    if loop_jump(ii) == 1
        continue
    end
    inter = intersection(lines(:,ii),lines(:,ii+1),corner(:,i),corner(:,i+1));
    if inter == 1
        loop_jump(ii) = 1;
        inter_point = intersection_point...
            (lines(:,ii),lines(:,ii+1),corner(:,i),corner(:,i+1));
        [matrix] = insert_line(matrix,inter_point,i);%last number:insert slot
        i2 = 4;
        inter2 = intersection(lines(:,ii),lines(:,ii+1),corner(:,i2),corner(:,i2+1));
        if inter2 == 1
            matrix(1,19) = 0;%delete point 4
            inter_point2 = intersection_point(lines(:,ii),lines(:,ii+1),corner(:,i2),corner(:,i2+1));
            [matrix] = insert_line(matrix,inter_point2,i2);%last number:insert slot
        else
            %only intersect with edge 1
            special_record_i = special_record_i+1;
            special_record(special_record_i,:) = [3,ii];%first number:code. second number:line
        end
    end
end

i=4;
for ii=1:lines_num
    if loop_jump(ii) == 1
        continue
    end
    inter = intersection(lines(:,ii),lines(:,ii+1),corner(:,i),corner(:,i+1));
    if inter == 1
        loop_jump(ii) = 1;
        inter_point = intersection_point...
            (lines(:,ii),lines(:,ii+1),corner(:,i),corner(:,i+1));
        [matrix] = insert_line(matrix,inter_point,i);%last number:insert slot
        %only intersect with edge 1
        special_record_i = special_record_i+1;
        special_record(special_record_i,:) = [4,ii];%first number:code. second number:line
    end
end

if special_record(1,1)>0.5
    %find the inner point
    ii = special_record(1,2);
    inner = [];
    if corner(1,1)<lines(1,ii)&&lines(1,ii)<corner(1,2)...
            &&corner(2,2)<lines(2,ii)&&lines(2,ii)<corner(2,3)
        inner = lines(:,ii);
    else
        ii = ii+1;
        if corner(1,1)<lines(1,ii)&&lines(1,ii)<corner(1,2)...
                &&corner(2,2)<lines(2,ii)&&lines(2,ii)<corner(2,3)
            inner = lines(:,ii);
        end
    end
    
    ii = special_record(2,2);
    if isempty(inner)
        if corner(1,1)<lines(1,ii)&&lines(1,ii)<corner(2,1)...
                &&corner(2,2)<lines(2,ii)&&lines(2,ii)<corner(2,3)
            inner = lines(:,ii);
        else
            ii = ii+1;
            if corner(1,1)<lines(1,ii)&&lines(1,ii)<corner(2,1)...
                    &&corner(2,2)<lines(2,ii)&&lines(2,ii)<corner(2,3)
                inner = lines(:,ii);
            end
        end
    end
    
    if special_record(1,1)==special_record(2,1) %situation:11/22/33/44
        matrix(1,1) = 0;%delete point 1
        matrix(1,7) = 0;%delete point 2
        matrix(1,13) = 0;%delete point 3
        matrix(1,19) = 0;%delete point 4
        %add inner point!!!!!!!!
        matrix(1,2) = 1;
        matrix(2:3,2) = inner;
    end
    if special_record(1,1)==1&&special_record(2,1)==2
        matrix(1,1) = 0;%delete point 1
        matrix(1,13) = 0;%delete point 3
        matrix(1,19) = 0;%delete point 4
        %add inner point!!!!!!!!
        matrix(1,2) = 1;
        matrix(2:3,2) = inner;
    end
    if special_record(1,1)==1&&special_record(2,1)==4
        matrix(1,7) = 0;%delete point 2
        matrix(1,13) = 0;%delete point 3
        matrix(1,19) = 0;%delete point 4
        %add inner point!!!!!!!!
        matrix(1,6) = 1;
        matrix(2:3,6) = inner;
    end
    if special_record(1,1)==2&&special_record(2,1)==3
        matrix(1,1) = 0;%delete point 1
        matrix(1,7) = 0;%delete point 2
        matrix(1,19) = 0;%delete point 4
        %add inner point!!!!!!!!
        matrix(1,8) = 1;
        matrix(2:3,8) = inner;
    end
    if special_record(1,1)==3&&special_record(2,1)==4
        matrix(1,1) = 0;%delete point 1
        matrix(1,7) = 0;%delete point 2
        matrix(1,13) = 0;%delete point 3
        %add inner point!!!!!!!!
        matrix(1,14) = 1;
        matrix(2:3,14) = inner;
    end
    if special_record(1,1)==1&&special_record(2,1)==3
        %judge intersection c2&line
        ii = special_record(1,2);
        judge_c1 = intersection(lines(:,ii),lines(:,ii+1),center,corner(:,2));
        ii = special_record(2,2);
        judge_c2 = intersection(lines(:,ii),lines(:,ii+1),center,corner(:,2));
        judge_c = judge_c1+judge_c2;
        if judge_c==0
            matrix(1,1) = 0;%delete point 1
            matrix(1,19) = 0;%delete point 4
            %add inner point!!!!!!!!
            matrix(1,2) = 1;
            matrix(2:3,2) = inner;
        else
            matrix(1,7) = 0;%delete point 2
            matrix(1,13) = 0;%delete point 3
            %add inner point!!!!!!!!
            matrix(1,6) = 1;
            matrix(2:3,6) = inner;
        end
    end
    if special_record(1,1)==2&&special_record(2,1)==4
        %judge intersection c2&line
        ii = special_record(1,2);
        judge_c1 = intersection(lines(:,ii),lines(:,ii+1),center,corner(:,2));
        ii = special_record(2,2);
        judge_c2 = intersection(lines(:,ii),lines(:,ii+1),center,corner(:,2));
        judge_c = judge_c1+judge_c2;
        if judge_c==0
            matrix(1,13) = 0;%delete point 3
            matrix(1,19) = 0;%delete point 4
            %add inner point!!!!!!!!
            matrix(1,12) = 1;
            matrix(2:3,12) = inner;
        else
            matrix(1,1) = 0;%delete point 1
            matrix(1,7) = 0;%delete point 2
            %add inner point!!!!!!!!
            matrix(1,8) = 1;
            matrix(2:3,8) = inner;
        end
    end
end

final_points = matrix(2:3,matrix(1,:)==1);
%make 25 same as 1!!!!!
final_points(:,length(final_points(1,:)))=final_points(:,1);
[ ~,area,~ ] = polygon_center_area( final_points,0 );

fraction = area/(sim.meshsize)^2;

end

function [matrix] = insert_line(matrix,inter_point,insert_slot)

num = (insert_slot-1)*6+4;
if insert_slot==1||insert_slot==3
    judge = inter_point(1,1);
else
    judge = inter_point(2,1);
end

if matrix(1,num)==0
    fin = num;
else
    if matrix(2,num)>judge
        fin=num-1;
    else
        fin=num+1;
    end
end
matrix(1,fin) = 1;
matrix(2:3,fin) = inter_point;

end